Skip to content
Merged

#43 #44

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

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 @@ -22,6 +22,7 @@ import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
import com.github.hauner.openapi.spring.converter.mapping.TypeMappingX
import com.github.hauner.openapi.spring.converter.schema.ArraySchemaType
import com.github.hauner.openapi.spring.converter.schema.ObjectSchemaType
import com.github.hauner.openapi.spring.converter.schema.PrimitiveSchemaType
import com.github.hauner.openapi.spring.converter.schema.SchemaInfo
import com.github.hauner.openapi.spring.converter.schema.SchemaType
import com.github.hauner.openapi.spring.model.DataTypes
Expand Down Expand Up @@ -154,7 +155,7 @@ class DataTypeConverter {

private DataType createSimpleDataType (SchemaInfo schemaInfo) {

TargetType targetType = getSimpleDataType (schemaInfo)
TargetType targetType = getMappedDataType (new PrimitiveSchemaType(schemaInfo))
if (targetType) {
def simpleType = new MappedDataType (
type: targetType.name,
Expand Down Expand Up @@ -247,37 +248,4 @@ class DataTypeConverter {
return match.targetType
}

private TargetType getSimpleDataType (SchemaInfo schemaInfo) {
if (options.typeMappings) {

// check global mapping
List<TypeMapping> mappings = getTypeMappingsY ()
List<TypeMapping> matches = mappings.findAll {
it.sourceTypeName == schemaInfo.type && it.sourceTypeFormat == schemaInfo.format
}

// no mapping, use default
if (matches.isEmpty ()) {
return null
}

if (matches.size () != 1) {
throw new AmbiguousTypeMappingException (matches)
}

def match = matches.first ()
return new TargetType (
typeName: match.targetTypeName,
genericNames: match.genericTypeNames ?: [])
}

null
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,20 @@ class ArraySchemaType extends BaseSchemaType {

}

class PrimitiveSchemaType extends BaseSchemaType {

PrimitiveSchemaType (SchemaInfo info) {
super (info)
}

@Override
List<TypeMappingX> matchTypeMapping (List<TypeMappingX> typeMappings) {
typeMappings.findAll () {
(it.isLevel (MappingLevel.TYPE)
// simple but ignores the interface!
&& it.sourceTypeName == info.type
&& it.sourceTypeFormat == info.format)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ paths:
}

@Unroll
void "converts array response schema to Collection<> via type" () {
void "converts array response schema to Collection<> via #type" () {
def openApi = parse ("""\
openapi: 3.0.2
info:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
package com.github.hauner.openapi.spring.converter

import com.github.hauner.openapi.spring.converter.mapping.AmbiguousTypeMappingException
import com.github.hauner.openapi.spring.converter.mapping.EndpointTypeMapping
import com.github.hauner.openapi.spring.converter.mapping.ParameterTypeMapping
import com.github.hauner.openapi.spring.converter.mapping.TypeMapping
import com.github.hauner.openapi.spring.model.Api
import spock.lang.Specification
import spock.lang.Unroll

import static com.github.hauner.openapi.spring.support.OpenApiParser.parse

class DataTypeConverterSimpleTypeMappingSpec extends Specification {
class DataTypeConverterPrimitiveTypeMappingSpec extends Specification {

void "converts basic types with format to java type via global type mapping" () {
def openApi = parse ("""\
Expand Down Expand Up @@ -108,4 +111,74 @@ paths:
e.typeMappings == options.typeMappings
}

@Unroll
void "converts primitive parameter schema to java type via #type" () {
def openApi = parse ("""\
openapi: 3.0.2
info:
title: API
version: 1.0.0

paths:
/foo:
get:
parameters:
- in: query
name: bar
required: false
schema:
type: string
format: date-time
responses:
'204':
description: none
""")

when:
def options = new ApiOptions(packageName: 'pkg', typeMappings: mappings)
Api api = new ApiConverter (options).convert (openApi)

then:
def itf = api.interfaces.first ()
def ep = itf.endpoints.first ()
def parameter = ep.parameters.first ()
parameter.dataType.packageName == 'java.time'
parameter.dataType.name == 'ZonedDateTime'

where:
type << [
'endpoint parameter mapping',
'global parameter mapping',
'global type mapping'
]

mappings << [
[
new EndpointTypeMapping (path: '/foo',
typeMappings: [
new ParameterTypeMapping (
parameterName: 'bar',
mapping: new TypeMapping (
sourceTypeName: 'string',
sourceTypeFormat: 'date-time',
targetTypeName: 'java.time.ZonedDateTime')
)
])
], [
new ParameterTypeMapping (
parameterName: 'bar',
mapping: new TypeMapping (
sourceTypeName: 'string',
sourceTypeFormat: 'date-time',
targetTypeName: 'java.time.ZonedDateTime')
)
], [
new TypeMapping (
sourceTypeName: 'string',
sourceTypeFormat: 'date-time',
targetTypeName: 'java.time.ZonedDateTime')
]
]
}

}