diff --git a/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy b/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy index 25c0e91b..8368c8f8 100644 --- a/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy +++ b/src/main/groovy/com/github/hauner/openapi/spring/converter/ApiConverter.groovy @@ -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) diff --git a/src/main/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverter.groovy b/src/main/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverter.groovy index 1fa37684..3f87579a 100644 --- a/src/main/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverter.groovy +++ b/src/main/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverter.groovy @@ -275,6 +275,20 @@ class DataTypeConverter { } } + // check endpoint type mapping + EndpointTypeMapping endpoint = getEndpointMappings ().find { it.path == schemaInfo.path } + if (endpoint) { + List 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 matches = options.typeMappings.findResults { it instanceof TypeMapping && it.sourceTypeName == schemaInfo.name ? it : null @@ -391,6 +405,12 @@ class DataTypeConverter { } } + private List getTypeMappings (List typeMappings) { + typeMappings.findResults { + it instanceof TypeMapping ? it : null + } + } + private List getTypeMappings () { options.typeMappings.findResults { it instanceof TypeMapping ? it : null diff --git a/src/main/groovy/com/github/hauner/openapi/spring/converter/ParameterSchemaInfo.groovy b/src/main/groovy/com/github/hauner/openapi/spring/converter/ParameterSchemaInfo.groovy index 7d86af85..fb6dac56 100644 --- a/src/main/groovy/com/github/hauner/openapi/spring/converter/ParameterSchemaInfo.groovy +++ b/src/main/groovy/com/github/hauner/openapi/spring/converter/ParameterSchemaInfo.groovy @@ -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 } diff --git a/src/main/groovy/com/github/hauner/openapi/spring/converter/ResponseSchemaInfo.groovy b/src/main/groovy/com/github/hauner/openapi/spring/converter/ResponseSchemaInfo.groovy index e34365ce..b200a714 100644 --- a/src/main/groovy/com/github/hauner/openapi/spring/converter/ResponseSchemaInfo.groovy +++ b/src/main/groovy/com/github/hauner/openapi/spring/converter/ResponseSchemaInfo.groovy @@ -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 } diff --git a/src/main/groovy/com/github/hauner/openapi/spring/converter/SchemaInfo.groovy b/src/main/groovy/com/github/hauner/openapi/spring/converter/SchemaInfo.groovy index 1df64d91..bbd58417 100644 --- a/src/main/groovy/com/github/hauner/openapi/spring/converter/SchemaInfo.groovy +++ b/src/main/groovy/com/github/hauner/openapi/spring/converter/SchemaInfo.groovy @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterObjectTypeMappingSpec.groovy b/src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterObjectTypeMappingSpec.groovy index a5bde087..5d197e11 100644 --- a/src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterObjectTypeMappingSpec.groovy +++ b/src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterObjectTypeMappingSpec.groovy @@ -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 @@ -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' + } + } diff --git a/src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterSpec.groovy b/src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterSpec.groovy index 38fc510b..ec263dff 100644 --- a/src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterSpec.groovy +++ b/src/test/groovy/com/github/hauner/openapi/spring/converter/DataTypeConverterSpec.groovy @@ -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 @@ -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) @@ -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