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

Commit cefc40f

Browse files
committed
#55, find endpoint/method type mapping
1 parent 80be4f8 commit cefc40f

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,17 @@ class MappingFinder(private val typeMappings: List<Mapping> = emptyList()) {
2222
* @throws AmbiguousTypeMappingException if there is more than one match.
2323
*/
2424
fun findEndpointTypeMapping(info: SchemaInfo): TypeMapping? {
25-
val ep = filterMappings(EndpointTypeMatcher(info.getPath(), info.getMethod()), typeMappings)
25+
// check with method
26+
val m = findEndpointTypeMapping(info, info.getMethod())
27+
if (m != null)
28+
return m
29+
30+
// check without method, i.e. all methods
31+
return findEndpointTypeMapping(info, null)
32+
}
33+
34+
private fun findEndpointTypeMapping(info: SchemaInfo, method: HttpMethod?): TypeMapping? {
35+
val ep = filterMappings(EndpointTypeMatcher(info.getPath(), method), typeMappings)
2636

2737
val parameter = getTypeMapping(filterMappings(ParameterTypeMatcher(info), ep))
2838
if (parameter != null)
@@ -84,7 +94,7 @@ class MappingFinder(private val typeMappings: List<Mapping> = emptyList()) {
8494
* @return the "result" type mappings or null if there is no match.
8595
*/
8696
fun findEndpointResultTypeMapping(info: SchemaInfo): ResultTypeMapping? {
87-
val ep = filterMappings(EndpointTypeMatcher(info.getPath(), info.getMethod()), typeMappings)
97+
val ep = filterMappings(EndpointTypeMatcher(info.getPath(), null/*info.getMethod()*/), typeMappings)
8898

8999
val matches = filterMappings({ _: ResultTypeMapping -> true }, ep)
90100
if (matches.isEmpty())
@@ -173,7 +183,7 @@ class MappingFinder(private val typeMappings: List<Mapping> = emptyList()) {
173183
fun isExcludedEndpoint(path: String, method: HttpMethod): Boolean {
174184
val ep = typeMappings
175185
.filterIsInstance<EndpointTypeMapping>()
176-
.filter(EndpointTypeMatcher(path, method))
186+
.filter(EndpointTypeMatcher(path, null/*method*/))
177187

178188
if (ep.isEmpty())
179189
return false

src/main/kotlin/io/openapiprocessor/core/converter/mapping/matcher/EndpointTypeMatcher.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import io.openapiprocessor.core.model.HttpMethod
1111
/**
1212
* [io.openapiprocessor.core.converter.mapping.MappingFinder] matcher for endpoint type mappings.
1313
*/
14-
class EndpointTypeMatcher(private val path: String, private val method: HttpMethod)
14+
class EndpointTypeMatcher(private val path: String, private val method: HttpMethod?)
1515
: (EndpointTypeMapping) -> Boolean {
1616

1717
override fun invoke(m: EndpointTypeMapping): Boolean {
18-
return m.path == path && (m.method == null || m.method == method)
18+
return m.path == path && m.method == method
1919
}
2020

2121
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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
7+
8+
import io.kotest.core.spec.style.StringSpec
9+
import io.kotest.matchers.nulls.shouldNotBeNull
10+
import io.kotest.matchers.shouldBe
11+
import io.mockk.mockk
12+
import io.openapiprocessor.core.converter.SchemaInfo
13+
import io.openapiprocessor.core.model.HttpMethod
14+
import io.openapiprocessor.core.parser.RefResolver
15+
16+
class MappingFinderEndpointMethodSpec: StringSpec({
17+
val resolver = mockk<RefResolver>()
18+
val foo = SchemaInfo.Endpoint("/foo", HttpMethod.GET)
19+
20+
"endpoint/method type mapping matches single mapping" {
21+
val finder = MappingFinder(
22+
listOf(
23+
EndpointTypeMapping("/foo", null, emptyList()),
24+
EndpointTypeMapping("/foo", HttpMethod.GET, listOf(
25+
TypeMapping("Foo", "io.openapiprocessor.Foo"),
26+
TypeMapping("Far", "io.openapiprocessor.Far"),
27+
TypeMapping("Bar", "io.openapiprocessor.Bar")
28+
)))
29+
)
30+
31+
val info = SchemaInfo(foo, "Foo", "", null, resolver)
32+
val result = finder.findEndpointTypeMapping(info)
33+
34+
result.shouldNotBeNull()
35+
result.sourceTypeName.shouldBe("Foo")
36+
result.targetTypeName.shouldBe("io.openapiprocessor.Foo")
37+
}
38+
39+
})

0 commit comments

Comments
 (0)