Skip to content
This repository was archived by the owner on Mar 16, 2025. It is now read-only.

Commit d995c7f

Browse files
committed
#45, find null mapping
1 parent 4a97816 commit d995c7f

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,35 @@ class MappingFinder(private val typeMappings: List<Mapping> = emptyList()) {
182182
return ep.first().exclude
183183
}
184184

185+
/**
186+
* find (endpoint) "null" type mappings.
187+
*
188+
* @param info schema info of the OpenAPI schema.
189+
* @return the "null" type mappings or null if there is no match.
190+
*/
191+
fun findEndpointNullTypeMapping(info: SchemaInfo): TypeMapping? {
192+
val ep = filterMappings(EndpointTypeMatcher(info.getPath()), typeMappings)
193+
194+
val matches = filterMappings(NullTypeMatcher(), ep)
195+
if (matches.isEmpty())
196+
return null
197+
198+
return matches.first() as TypeMapping
199+
}
200+
201+
/**
202+
* find (global) "null" type mapping.
203+
*
204+
* @return the "multi" type mappings or null if there is no match.
205+
*/
206+
fun findNullTypeMapping(): TypeMapping? {
207+
val matches = filterMappings(NullTypeMatcher(), typeMappings)
208+
if (matches.isEmpty())
209+
return null
210+
211+
return matches.first() as TypeMapping
212+
}
213+
185214
private fun getTypeMapping(mappings: List<Mapping>): TypeMapping? {
186215
if (mappings.isEmpty())
187216
return null
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2021 https://github.com/openapi-processor/openapi-processor-core
3+
* PDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.openapiprocessor.core.converter.mapping.matcher
7+
8+
import io.openapiprocessor.core.converter.mapping.TypeMapping
9+
10+
/**
11+
* [io.openapiprocessor.core.converter.mapping.MappingFinder] matcher for null type mapping.
12+
*/
13+
class NullTypeMatcher: (TypeMapping) -> Boolean {
14+
15+
override fun invoke(mapping: TypeMapping): Boolean {
16+
return mapping.sourceTypeName == "null"
17+
}
18+
19+
}

src/test/kotlin/io/openapiprocessor/core/converter/mapping/MappingFinderSpec.kt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,4 +248,31 @@ class MappingFinderSpec: StringSpec({
248248
result.targetTypeName.shouldBe("io.openapiprocessor.Foo")
249249
}
250250

251+
"no endpoint null mapping in empty mappings" {
252+
val finder = MappingFinder(emptyList())
253+
254+
val info = SchemaInfo("/foo", "", "", null, resolver)
255+
val result = finder.findEndpointNullTypeMapping(info)
256+
257+
result.shouldBeNull()
258+
}
259+
260+
"endpoint type mapping matches null mapping" {
261+
val finder = MappingFinder(
262+
listOf(
263+
EndpointTypeMapping("/foo", listOf(
264+
TypeMapping("null", "org.openapitools.jackson.nullable.JsonNullable"),
265+
TypeMapping("Far", "io.openapiprocessor.Far"),
266+
TypeMapping("Bar", "io.openapiprocessor.Bar")
267+
)))
268+
)
269+
270+
val info = SchemaInfo("/foo", "Foo", "", null, resolver)
271+
val result = finder.findEndpointNullTypeMapping(info)
272+
273+
result.shouldNotBeNull()
274+
result.sourceTypeName.shouldBe("null")
275+
result.targetTypeName.shouldBe("org.openapitools.jackson.nullable.JsonNullable")
276+
}
277+
251278
})

0 commit comments

Comments
 (0)