Skip to content

Commit f4172c9

Browse files
committed
support result-style on all mapping levels (#253)
1 parent 3f62edf commit f4172c9

6 files changed

Lines changed: 61 additions & 6 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ class EndpointMappings(
3333
}
3434

3535
fun getResultStyle(query: MappingQuery, step: MappingStep): ResultStyle? {
36+
val httpMethodMappings = methodMappings[query.method]
37+
if (httpMethodMappings != null) {
38+
val methodMapping = httpMethodMappings.getResultStyle(step.add(MethodsStep(query)))
39+
if (methodMapping != null) {
40+
return methodMapping
41+
}
42+
}
43+
3644
val mapping = mappings.getResultStyle(step)
3745
if (mapping != null) {
3846
return mapping

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/Map.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ data class Map(
1818
val result: String? = null,
1919

2020
/**
21-
* controller method return type, eg. **success** response or **all** responses
21+
* controller method return type, e.g. **success** response or **all** responses
2222
*/
2323
val resultStyle: ResultStyle? = null,
2424

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/MappingConverter.kt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ class MappingConverter(val mapping: MappingV2) {
319319

320320
private fun convertPath(source: Path): Mappings {
321321
var resultTypeMapping: ResultTypeMapping? = null
322+
var resultStyle: ResultStyle? = null
322323
var singleTypeMapping: TypeMapping? = null
323324
var multiTypeMapping: TypeMapping? = null
324325
var nullTypeMapping: NullTypeMapping? = null
@@ -331,6 +332,10 @@ class MappingConverter(val mapping: MappingV2) {
331332
resultTypeMapping = convertResult(source.result)
332333
}
333334

335+
if (source.resultStyle != null) {
336+
resultStyle = source.resultStyle
337+
}
338+
334339
if(source.single != null) {
335340
singleTypeMapping = convertType("single" , source.single)
336341
}
@@ -361,7 +366,7 @@ class MappingConverter(val mapping: MappingV2) {
361366

362367
return Mappings(
363368
resultTypeMapping,
364-
null,
369+
resultStyle,
365370
singleTypeMapping,
366371
multiTypeMapping,
367372
nullTypeMapping,
@@ -374,6 +379,7 @@ class MappingConverter(val mapping: MappingV2) {
374379

375380
private fun convertPathMethod(source: PathMethod): Mappings {
376381
var resultTypeMapping: ResultTypeMapping? = null
382+
var resultStyle: ResultStyle? = null
377383
var singleTypeMapping: TypeMapping? = null
378384
var multiTypeMapping: TypeMapping? = null
379385
var nullTypeMapping: NullTypeMapping? = null
@@ -386,9 +392,9 @@ class MappingConverter(val mapping: MappingV2) {
386392
resultTypeMapping = convertResult(source.result)
387393
}
388394

389-
// if (source.resultStyle != null) {
390-
// resultStyle = source.resultStyle
391-
// }
395+
if (source.resultStyle != null) {
396+
resultStyle = source.resultStyle
397+
}
392398

393399
if(source.single != null) {
394400
singleTypeMapping = convertType("single" , source.single)
@@ -420,7 +426,7 @@ class MappingConverter(val mapping: MappingV2) {
420426

421427
return Mappings(
422428
resultTypeMapping,
423-
null,
429+
resultStyle,
424430
singleTypeMapping,
425431
multiTypeMapping,
426432
nullTypeMapping,

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/Path.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ data class Path(
3333
*/
3434
val result: String?,
3535

36+
/**
37+
* controller method return type, e.g. **success** response or **all** responses
38+
*/
39+
val resultStyle: ResultStyle? = null,
40+
3641
/**
3742
* single mapping, i.e. Mono<>
3843
*/

openapi-processor-core/src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/PathMethod.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class PathMethod(
2020
*/
2121
val result: String?,
2222

23+
/**
24+
* controller method return type, e.g. **success** response or **all** responses
25+
*/
26+
val resultStyle: ResultStyle? = null,
27+
2328
/**
2429
* single mapping, i.e. Mono<>
2530
*/

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,35 @@ class MappingConverterResultSpec: StringSpec({
159159
resultTypeMappingGet.targetTypeName shouldBe "io.openapiprocessor.WrapGet"
160160
resultTypeMappingGet.genericTypes.shouldBeEmpty()
161161
}
162+
163+
"read endpoint result style" {
164+
val yaml = """
165+
|openapi-processor-mapping: $VERSION
166+
|
167+
|options:
168+
| package-name: io.openapiprocessor.somewhere
169+
|
170+
|map:
171+
| paths:
172+
| /foo:
173+
| result-style: success
174+
|
175+
| get:
176+
| result-style: all
177+
|
178+
""".trimMargin()
179+
180+
// when:
181+
val mapping = reader.read (yaml) as Mapping
182+
val mappings = MappingConverter(mapping).convert().endpointMappings
183+
184+
// then:
185+
val resultQuery = MappingFinderQuery(path = "/foo", method = HttpMethod.POST)
186+
val resultStyle = mappings["/foo"]!!.getResultStyle(resultQuery, EndpointsStep(resultQuery))!!
187+
resultStyle.shouldBe(ResultStyle.SUCCESS)
188+
189+
val resultQueryGet = MappingFinderQuery(path = "/foo", method = HttpMethod.GET)
190+
val resultStyleGet = mappings["/foo"]!!.getResultStyle(resultQueryGet, EndpointsStep(resultQueryGet))!!
191+
resultStyleGet.shouldBe(ResultStyle.ALL)
192+
}
162193
})

0 commit comments

Comments
 (0)