Skip to content
Merged

#39 #40

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ApiConverter {
def contentType = requestBodyEntry.key
def requestBody = requestBodyEntry.value

def info = new SchemaInfo (requestBody.schema, getInlineTypeName (path))
def info = new SchemaInfo (path, requestBody.schema, getInlineTypeName (path))
info.resolver = resolver

DataType dataType = dataTypeConverter.convert (info, target.models)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,20 @@ class DataTypeConverter {
}
}

// check endpoint type mapping
EndpointTypeMapping endpoint = getEndpointMappings ().find { it.path == schemaInfo.path }
if (endpoint) {
List<TypeMapping> mappings = getTypeMappings (endpoint.typeMappings)

def mapping = mappings.find { it.sourceTypeName == schemaInfo.name ? it : null }
if (mapping) {
return new TargetType (
typeName: mapping.targetTypeName,
genericNames: mapping.genericTypeNames
)
}
}

// check global mapping
List<TypeMapping> matches = options.typeMappings.findResults {
it instanceof TypeMapping && it.sourceTypeName == schemaInfo.name ? it : null
Expand Down Expand Up @@ -391,6 +405,12 @@ class DataTypeConverter {
}
}

private List<TypeMapping> getTypeMappings (List<?> typeMappings) {
typeMappings.findResults {
it instanceof TypeMapping ? it : null
}
}

private List<TypeMapping> getTypeMappings () {
options.typeMappings.findResults {
it instanceof TypeMapping ? it : null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,13 @@ import io.swagger.v3.oas.models.media.Schema
*/
class ParameterSchemaInfo extends SchemaInfo {

/**
* Endpoint path.
*/
String path

/**
* Parameter name.
*/
String parameterName

ParameterSchemaInfo (String path, Schema schema, String name) {
super (schema, name)
this.path = path
super (path, schema, name)
this.parameterName = name
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,13 @@ import io.swagger.v3.oas.models.media.Schema
*/
class ResponseSchemaInfo extends SchemaInfo {

/**
* Endpoint path.
*/
String path

/**
* Response content type.
*/
String contentType

ResponseSchemaInfo (String path, String contentType, Schema schema, String name) {
super (schema, name)
this.path = path
super (path, schema, name)
this.contentType = contentType
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,29 @@ import io.swagger.v3.oas.models.media.Schema
* @author Martin Hauner
*/
class SchemaInfo {
Schema schema

/**
* Endpoint path.
*/
String path

/**
* name of the type/schema.
*/
String name

/**
* the OpenAPI schema
*/
Schema schema

/**
* resolver of $ref'erences
*/
RefResolver resolver

SchemaInfo (Schema schema, String name) {
SchemaInfo (String path, Schema schema, String name) {
this.path = path
this.schema = schema
this.name = name
}
Expand All @@ -50,7 +67,7 @@ class SchemaInfo {
SchemaInfo buildForRef () {
def refName = getRefName (schema)
Schema refSchema = resolver.resolve (schema.$ref)
def info = new SchemaInfo(refSchema, refName)
def info = new SchemaInfo (path, refSchema, refName)
info.resolver = resolver
info
}
Expand All @@ -64,7 +81,7 @@ class SchemaInfo {
* @return a new DataTypeInfo
*/
SchemaInfo buildForNestedType (String nestedName, Schema nestedSchema) {
def info = new SchemaInfo (nestedSchema, getNestedTypeName (nestedName))
def info = new SchemaInfo (path, nestedSchema, getNestedTypeName (nestedName))
info.resolver = resolver
info
}
Expand All @@ -75,7 +92,7 @@ class SchemaInfo {
* @return s new DataTypeInfo
*/
SchemaInfo buildForItem () {
def info = new SchemaInfo (schema.items, name)
def info = new SchemaInfo (path, schema.items, name)
info.resolver = resolver
info
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.github.hauner.openapi.spring.converter

import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
import com.github.hauner.openapi.spring.model.Api
import spock.lang.Specification
Expand Down Expand Up @@ -162,4 +163,73 @@ components:
e.typeMappings == options.typeMappings
}

void "converts named schemas to java type via endpoint type mapping" () {
def openApi = parse ("""\
openapi: 3.0.2
info:
title: API
version: 1.0.0

paths:
/foobar:
get:
parameters:
- in: query
name: foo
required: false
schema:
\$ref: '#/components/schemas/Foo'
responses:
'200':
description: none
content:
application/json:
schema:
\$ref: '#/components/schemas/Bar'

components:
schemas:

Foo:
description: minimal query parameter object
type: object
properties:
foo:
type: string

Bar:
description: minimal response object
type: object
properties:
bar:
type: string

""")

when:
def options = new ApiOptions(
packageName: 'pkg',
typeMappings: [
new EndpointTypeMapping (path: '/foobar',
typeMappings: [
new TypeMapping (
sourceTypeName: 'Foo',
targetTypeName: 'someA.ObjectA'),
new TypeMapping (
sourceTypeName: 'Bar',
targetTypeName: 'someB.ObjectB')])
])
Api api = new ApiConverter (options).convert (openApi)

then:
def itf = api.interfaces.first ()
def ep = itf.endpoints.first ()
def parameter = ep.parameters.first ()
def response = ep.response
parameter.dataType.packageName == 'someA'
parameter.dataType.name == 'ObjectA'
response.responseType.packageName == 'someB'
response.responseType.name == 'ObjectB'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class DataTypeConverterSpec extends Specification {
Schema schema = new Schema(type: type, format: format)

when:
def datatype = converter.convert (new SchemaInfo (schema, javaType), new DataTypes())
def datatype = converter.convert (new SchemaInfo (null, schema, javaType), new DataTypes())

then:
datatype.name == javaType
Expand All @@ -67,7 +67,7 @@ class DataTypeConverterSpec extends Specification {
Schema schema = new Schema(type: type, format: format)

when:
converter.convert (new SchemaInfo(schema, null), new DataTypes())
converter.convert (new SchemaInfo (null, schema, null), new DataTypes())

then:
def e = thrown(UnknownDataTypeException)
Expand All @@ -91,8 +91,8 @@ class DataTypeConverterSpec extends Specification {
])

when:
converter.convert (new SchemaInfo (barSchema, 'Bar'), dt)
converter.convert (new SchemaInfo (fooSchema, 'Foo'), dt)
converter.convert (new SchemaInfo (null, barSchema, 'Bar'), dt)
converter.convert (new SchemaInfo (null, fooSchema, 'Foo'), dt)

then:
assert dt.size () == 2
Expand Down