Skip to content

Commit 80dc31a

Browse files
committed
add interface parameter mapping (#346)
1 parent ce87e11 commit 80dc31a

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,26 @@ class EndpointMappings(
297297
return emptyList()
298298
}
299299

300+
fun findInterfaceParameterTypeMapping(query: MappingQuery, step: MappingStep): List<InterfaceTypeMapping> {
301+
val httpMethodMappings = methodMappings[query.method]
302+
if (httpMethodMappings != null) {
303+
val methodMapping = httpMethodMappings.findInterfaceParameterTypeMapping(
304+
InterfaceTypeMatcher(query),
305+
step.add(MethodsStep(query)))
306+
307+
if (methodMapping.isNotEmpty()) {
308+
return methodMapping
309+
}
310+
}
311+
312+
val mapping = mappings.findInterfaceParameterTypeMapping(InterfaceTypeMatcher(query), step)
313+
if (mapping.isNotEmpty()) {
314+
return mapping
315+
}
316+
317+
return emptyList()
318+
}
319+
300320
fun isExcluded(query: MappingQuery, step: MappingStep): Boolean {
301321
val httpMethodMappings = methodMappings[query.method]
302322
if (httpMethodMappings != null) {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ class Mappings(
129129
return mappings.first() as TypeMapping
130130
}
131131

132+
fun findInterfaceParameterTypeMapping(filter: MappingMatcher, step: MappingStep): List<InterfaceTypeMapping> {
133+
val mappings = parameterTypeMappings.filter(filter, step.add(ParametersStep("type")))
134+
if (mappings.isEmpty()) {
135+
return emptyList()
136+
}
137+
138+
return mappings.map { it as InterfaceTypeMapping }
139+
}
140+
132141
fun findAnnotationParameterTypeMapping(filter: MappingMatcher, step: MappingStep): List<AnnotationTypeMapping> {
133142
val mappings = parameterTypeMappings.filter(filter, step.add(ParametersStep("type")))
134143
if (mappings.isEmpty()) {

openapi-processor-core/src/test/kotlin/io/openapiprocessor/core/processor/mapping/v2/MappingConverterInterfacesSpec.kt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.kotest.matchers.collections.shouldHaveSize
1010
import io.kotest.matchers.shouldBe
1111
import io.openapiprocessor.core.converter.MappingFinderQuery
1212
import io.openapiprocessor.core.converter.mapping.matcher.InterfaceTypeMatcher
13+
import io.openapiprocessor.core.converter.mapping.steps.ParametersStep
1314
import io.openapiprocessor.core.converter.mapping.steps.TypesStep
1415
import io.openapiprocessor.core.parser.HttpMethod
1516
import io.openapiprocessor.core.processor.MappingReader
@@ -46,6 +47,34 @@ class MappingConverterInterfacesSpec: StringSpec({
4647
typeMappings[1].genericTypes[0].typeName shouldBe "java.lang.String"
4748
}
4849

50+
"read global interface parameter type mapping" {
51+
val yaml = """
52+
|openapi-processor-mapping: $VERSION
53+
|
54+
|options:
55+
| package-name: io.openapiprocessor.somewhere
56+
|
57+
|map:
58+
| parameters:
59+
| - type: Foo =+ java.io.Serializable
60+
| - type: Foo =+ some.other.Interface<java.lang.String>
61+
""".trimMargin()
62+
63+
val mapping = reader.read(yaml) as Mapping
64+
val mappings = MappingConverter(mapping).convert().globalMappings
65+
66+
val query = MappingFinderQuery(path = "/foo", name = "Foo")
67+
val typeMappings = mappings.findInterfaceParameterTypeMapping(
68+
InterfaceTypeMatcher(query), ParametersStep("type"))
69+
70+
typeMappings shouldHaveSize 2
71+
typeMappings[0].sourceTypeName shouldBe "Foo"
72+
typeMappings[0].targetTypeName shouldBe "java.io.Serializable"
73+
typeMappings[1].sourceTypeName shouldBe "Foo"
74+
typeMappings[1].targetTypeName shouldBe "some.other.Interface"
75+
typeMappings[1].genericTypes[0].typeName shouldBe "java.lang.String"
76+
}
77+
4978
"read endpoint interface mappings" {
5079
val yaml = """
5180
|openapi-processor-mapping: $VERSION
@@ -75,6 +104,35 @@ class MappingConverterInterfacesSpec: StringSpec({
75104
typeMappings[1].genericTypes[0].typeName shouldBe "java.lang.String"
76105
}
77106

107+
"read endpoint parameter interface mappings" {
108+
val yaml = """
109+
|openapi-processor-mapping: $VERSION
110+
|
111+
|options:
112+
| package-name: io.openapiprocessor.somewhere
113+
|
114+
|map:
115+
| paths:
116+
| /foo:
117+
| parameters:
118+
| - type: Foo =+ java.io.Serializable
119+
| - type: Foo =+ some.other.Interface<java.lang.String>
120+
""".trimMargin()
121+
122+
val mapping = reader.read (yaml) as Mapping
123+
val mappings = MappingConverter(mapping).convert().endpointMappings
124+
125+
val query = MappingFinderQuery(path = "/foo", name = "Foo")
126+
val typeMappings = mappings["/foo"]!!.findInterfaceParameterTypeMapping(query, TypesStep())
127+
128+
typeMappings shouldHaveSize 2
129+
typeMappings[0].sourceTypeName shouldBe "Foo"
130+
typeMappings[0].targetTypeName shouldBe "java.io.Serializable"
131+
typeMappings[1].sourceTypeName shouldBe "Foo"
132+
typeMappings[1].targetTypeName shouldBe "some.other.Interface"
133+
typeMappings[1].genericTypes[0].typeName shouldBe "java.lang.String"
134+
}
135+
78136
"read endpoint/method interface mappings" {
79137
val yaml = """
80138
|openapi-processor-mapping: $VERSION

0 commit comments

Comments
 (0)