Skip to content

Commit 73fdc12

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

File tree

6 files changed

+57
-7
lines changed

6 files changed

+57
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class DataTypeConverter(
293293
}
294294

295295
private fun addInterfaces(schemaInfo: SchemaInfo, obj: ModelDataType) {
296-
finder.findInterfaceTypeMappings(MappingFinderQuery(schemaInfo)).forEach { mapping ->
296+
finder.findAnyInterfaceTypeMapping(MappingFinderQuery(schemaInfo)).forEach { mapping ->
297297
val targetType = mapping.getTargetType()
298298
val theInterface = InterfaceDataTypeExisting(DataTypeName(targetType.getName()), targetType.getPkg())
299299
obj.addInterface(theInterface)

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,47 @@ class MappingFinder(val options: ApiOptions) {
202202
return null
203203
}
204204

205+
/**
206+
* find any interface mapping. The mappings are checked in the following order and the first match wins:
207+
*
208+
* - endpoint parameter type
209+
* - endpoint type
210+
* - global parameter type
211+
* - global type
212+
*/
213+
fun findAnyInterfaceTypeMapping(query: MappingQuery): List<InterfaceTypeMapping> {
214+
val step = rootStep("looking for interface type mappings of", query)
215+
try {
216+
return findAnyInterfaceTypeMapping(query, step)
217+
} finally {
218+
step.log()
219+
}
220+
}
221+
222+
fun findAnyInterfaceTypeMapping(query: MappingQuery, step: MappingStep): List<InterfaceTypeMapping> {
223+
val eppMapping = repository.findEndpointInterfaceParameterTypeMappings(query, step)
224+
if (eppMapping.isNotEmpty()) {
225+
return eppMapping
226+
}
227+
228+
val eptMapping = repository.findEndpointInterfaceTypeMappings(query, step)
229+
if (eptMapping.isNotEmpty()) {
230+
return eptMapping
231+
}
232+
233+
val gpMapping = repository.findGlobalInterfaceParameterTypeMappings(query, step)
234+
if (gpMapping.isNotEmpty()) {
235+
return gpMapping
236+
}
237+
238+
val gtMapping = repository.findGlobalInterfaceTypeMappings(query, step)
239+
if (gtMapping.isNotEmpty()) {
240+
return gtMapping
241+
}
242+
243+
return emptyList()
244+
}
245+
205246
// path/method/name/format/type
206247
fun findTypeMapping(query: MappingQuery): TypeMapping? {
207248
val step = rootStep("looking for type mapping of", query)
@@ -286,7 +327,7 @@ class MappingFinder(val options: ApiOptions) {
286327
}
287328

288329
private fun findInterfaceTypeMapping(query: MappingQuery, step: MappingStep): List<InterfaceTypeMapping> {
289-
val epMapping = repository.findEndpointInterfaceTypeMapping(query, step)
330+
val epMapping = repository.findEndpointInterfaceTypeMappings(query, step)
290331
if (epMapping.isNotEmpty()) {
291332
return epMapping
292333
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ class EndpointMappings(
300300
fun findInterfaceParameterTypeMapping(query: MappingQuery, step: MappingStep): List<InterfaceTypeMapping> {
301301
val httpMethodMappings = methodMappings[query.method]
302302
if (httpMethodMappings != null) {
303-
val methodMapping = httpMethodMappings.findInterfaceParameterTypeMapping(
303+
val methodMapping = httpMethodMappings.findInterfaceParameterTypeMappings(
304304
InterfaceTypeMatcher(query),
305305
step.add(MethodsStep(query)))
306306

@@ -309,7 +309,7 @@ class EndpointMappings(
309309
}
310310
}
311311

312-
val mapping = mappings.findInterfaceParameterTypeMapping(InterfaceTypeMatcher(query), step)
312+
val mapping = mappings.findInterfaceParameterTypeMappings(InterfaceTypeMatcher(query), step)
313313
if (mapping.isNotEmpty()) {
314314
return mapping
315315
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ class MappingRepository(
5858
return globalMappings.findParameterTypeMapping(TypeMatcher(query), step.add(GlobalsStep()))
5959
}
6060

61+
fun findGlobalInterfaceParameterTypeMappings(query: MappingQuery, step: MappingStep): List<InterfaceTypeMapping> {
62+
return globalMappings.findInterfaceParameterTypeMappings(InterfaceTypeMatcher(query), step.add(GlobalsStep()))
63+
}
64+
6165
fun findGlobalAnnotationParameterTypeMappings(query: MappingQuery, step: MappingStep): List<AnnotationTypeMapping> {
6266
return globalMappings.findAnnotationParameterTypeMapping(AnnotationTypeMatcher(query), step.add(GlobalsStep()))
6367
}
@@ -111,11 +115,16 @@ class MappingRepository(
111115
return endpointMappings[query.path]?.findTypeMapping(query, step.add(EndpointsStep(query)))
112116
}
113117

114-
fun findEndpointInterfaceTypeMapping(query: MappingQuery, step: MappingStep): List<InterfaceTypeMapping> {
118+
fun findEndpointInterfaceTypeMappings(query: MappingQuery, step: MappingStep): List<InterfaceTypeMapping> {
115119
val mappings = endpointMappings[query.path] ?: return emptyList()
116120
return mappings.findInterfaceTypeMappings(query, step.add(EndpointsStep(query)))
117121
}
118122

123+
fun findEndpointInterfaceParameterTypeMappings(query: MappingQuery, step: MappingStep): List<InterfaceTypeMapping> {
124+
val mappings = endpointMappings[query.path] ?: return emptyList()
125+
return mappings.findInterfaceParameterTypeMapping(query, step.add(EndpointsStep(query)))
126+
}
127+
119128
fun findEndpointAnnotationTypeMapping(query: MappingQuery, step: MappingStep): List<AnnotationTypeMapping> {
120129
val pathMappings = endpointMappings[query.path] ?: return emptyList()
121130
return pathMappings.findAnnotationTypeMappings(query, step.add(EndpointsStep(query)))

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class Mappings(
129129
return mappings.first() as TypeMapping
130130
}
131131

132-
fun findInterfaceParameterTypeMapping(filter: MappingMatcher, step: MappingStep): List<InterfaceTypeMapping> {
132+
fun findInterfaceParameterTypeMappings(filter: MappingMatcher, step: MappingStep): List<InterfaceTypeMapping> {
133133
val mappings = parameterTypeMappings.filter(filter, step.add(ParametersStep("type")))
134134
if (mappings.isEmpty()) {
135135
return emptyList()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class MappingConverterInterfacesSpec: StringSpec({
6464
val mappings = MappingConverter(mapping).convert().globalMappings
6565

6666
val query = MappingFinderQuery(path = "/foo", name = "Foo")
67-
val typeMappings = mappings.findInterfaceParameterTypeMapping(
67+
val typeMappings = mappings.findInterfaceParameterTypeMappings(
6868
InterfaceTypeMatcher(query), ParametersStep("type"))
6969

7070
typeMappings shouldHaveSize 2

0 commit comments

Comments
 (0)