Skip to content

Commit aa3160e

Browse files
authored
Merge pull request #33 from openapi-processor/mapping-check-format
check on primitive could ignore the format
2 parents 77ccf64 + 59f94e4 commit aa3160e

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/converter/mapping/MappingFinder.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,24 +284,31 @@ class IoMatcher(schema: MappingSchema): BaseVisitor(schema) {
284284
class TypeMatcher(schema: MappingSchema): BaseVisitor(schema) {
285285

286286
override fun match(mapping: TypeMapping): Boolean {
287-
if (mapping.sourceTypeName == schema.getName()) {
287+
// try to match by name first, the format must match to avoid matching primitive
288+
// and primitive with format e.g. string should not match string:binary
289+
if (matchesName(mapping) && matchesFormat(mapping)) {
288290
return true
289291
}
290292

291293
return when {
292294
schema.isPrimitive() -> {
293-
mapping.sourceTypeName == schema.getType()
294-
&& mapping.sourceTypeFormat == schema.getFormat()
295+
matchesType(mapping) && matchesFormat(mapping)
295296
}
296297
schema.isArray() -> {
297-
mapping.sourceTypeName == "array"
298+
matchesArray(mapping)
298299
}
299300
else -> {
301+
// nop
300302
false
301303
}
302304
}
303305
}
304306

307+
private fun matchesName(mapping: TypeMapping): Boolean = mapping.sourceTypeName == schema.getName()
308+
private fun matchesType(mapping: TypeMapping): Boolean = mapping.sourceTypeName == schema.getType()
309+
private fun matchesArray(mapping: TypeMapping): Boolean = mapping.sourceTypeName == "array"
310+
private fun matchesFormat(mapping: TypeMapping): Boolean = mapping.sourceTypeFormat == schema.getFormat()
311+
305312
}
306313

307314
class ResultTypeMatcher(schema: MappingSchema): BaseVisitor(schema) {

openapi-processor-core/src/test/groovy/com/github/hauner/openapi/core/converter/DataTypeConverterPrimitiveTypeMappingSpec.groovy

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,56 @@ paths:
7676
parameter.dataType.name == 'ZonedDateTime'
7777
}
7878

79+
void "primitive type dose not match primitive global type mapping with format" () {
80+
def openApi = parse ("""\
81+
openapi: 3.0.2
82+
83+
info:
84+
title: API
85+
version: 1.0.0
86+
87+
paths:
88+
/foo:
89+
get:
90+
parameters:
91+
- in: query
92+
name: foo
93+
schema:
94+
type: array
95+
items:
96+
type: string
97+
responses:
98+
200:
99+
description: response
100+
content:
101+
application/*:
102+
schema:
103+
type: string
104+
format: binary
105+
106+
""")
107+
108+
when:
109+
def options = new ApiOptions(
110+
packageName: 'pkg',
111+
typeMappings: [
112+
new TypeMapping (
113+
'string',
114+
'binary',
115+
'io.openapiprocessor.Bar')
116+
])
117+
118+
Api api = new ApiConverter (options, new FrameworkBase ())
119+
.convert (openApi)
120+
121+
then:
122+
def itf = api.interfaces.first ()
123+
def ep = itf.endpoints.first ()
124+
def parameter = ep.parameters.first ()
125+
parameter.dataType.packageName == 'java.lang'
126+
parameter.dataType.name == 'String[]'
127+
}
128+
79129
void "converts named primitive type to java type via global type mapping" () {
80130
def openApi = parse ("""\
81131
openapi: 3.0.2

0 commit comments

Comments
 (0)