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

Commit 885810b

Browse files
committed
read option & improve name, #68
1 parent f9b7c28 commit 885810b

File tree

6 files changed

+63
-10
lines changed

6 files changed

+63
-10
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
/**
9+
* allows to handle simple name/value options in the mappings configuration.
10+
*/
11+
class OptionMapping<T>(val name: String, val value: T): Mapping {
12+
13+
override fun getChildMappings(): List<Mapping> {
14+
return listOf(this)
15+
}
16+
17+
}

src/main/kotlin/io/openapiprocessor/core/processor/MappingReader.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.openapiprocessor.core.processor
77

88
import com.fasterxml.jackson.databind.DeserializationFeature
9+
import com.fasterxml.jackson.databind.MapperFeature
910
import com.fasterxml.jackson.databind.ObjectMapper
1011
import com.fasterxml.jackson.databind.PropertyNamingStrategies
1112
import com.fasterxml.jackson.databind.module.SimpleModule
@@ -84,10 +85,11 @@ class MappingReader(private val validator: MappingValidator = MappingValidator()
8485
.nullIsSameAsDefault (true)
8586
.build ()
8687

87-
return ObjectMapper (YAMLFactory ())
88-
.configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
89-
.setPropertyNamingStrategy (PropertyNamingStrategies.KEBAB_CASE)
90-
.registerModules (kotlinModule, module)
88+
return ObjectMapper(YAMLFactory())
89+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
90+
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true)
91+
.setPropertyNamingStrategy(PropertyNamingStrategies.KEBAB_CASE)
92+
.registerModules(kotlinModule, module)
9193
}
9294

9395
private fun createV1Parser(): ObjectMapper {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ data class Map(
1515
*/
1616
val result: String? = null,
1717

18+
/**
19+
* controller method return type, eg. **success** response or **all** responses
20+
*/
21+
val resultStyle: ResultStyle? = null,
22+
1823
/**
1924
* single mapping, e.g. Mono<>
2025
*/
@@ -25,11 +30,6 @@ data class Map(
2530
*/
2631
val multi: String? = null,
2732

28-
/**
29-
* controller method response
30-
*/
31-
val responseType: ResponseType? = ResponseType.ALL,
32-
3333
/**
3434
* null wrapper, e.g. JsonNullable<>
3535
*/

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class MappingConverter(val mapping: MappingV2) {
4747
result.add(convertResult(mapping.map.result))
4848
}
4949

50+
if(mapping.map.resultStyle != null) {
51+
result.add(convertOption("resultStyle", mapping.map.resultStyle))
52+
}
53+
5054
if(mapping.map.single != null) {
5155
result.add(convertType("single" , mapping.map.single))
5256
}
@@ -79,6 +83,10 @@ class MappingConverter(val mapping: MappingV2) {
7983
return result
8084
}
8185

86+
private fun <T> convertOption(name: String, value: T): Mapping {
87+
return OptionMapping(name, value)
88+
}
89+
8290
private fun convertResult (result: String): Mapping {
8391
return ResultTypeMapping(result)
8492
}

src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/ResponseType.kt renamed to src/main/kotlin/io/openapiprocessor/core/processor/mapping/v2/ResultStyle.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package io.openapiprocessor.core.processor.mapping.v2
77

8-
enum class ResponseType(val kind: String) {
8+
enum class ResultStyle(val kind: String) {
99
ALL("all"),
1010
SUCCESS("success")
1111
}

src/test/groovy/com/github/hauner/openapi/core/processor/MappingConverterV2Spec.groovy

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ package com.github.hauner.openapi.core.processor
1818

1919
import io.openapiprocessor.core.converter.mapping.AddParameterTypeMapping
2020
import io.openapiprocessor.core.converter.mapping.EndpointTypeMapping
21+
import io.openapiprocessor.core.converter.mapping.OptionMapping
2122
import io.openapiprocessor.core.converter.mapping.ParameterTypeMapping
2223
import io.openapiprocessor.core.converter.mapping.ResponseTypeMapping
2324
import io.openapiprocessor.core.converter.mapping.ResultTypeMapping
2425
import io.openapiprocessor.core.converter.mapping.TypeMapping
2526
import io.openapiprocessor.core.processor.MappingConverter
2627
import io.openapiprocessor.core.processor.MappingReader
28+
import io.openapiprocessor.core.processor.mapping.v2.ResultStyle
2729
import spock.lang.Specification
2830
import spock.lang.Subject
2931
import spock.lang.Unroll
@@ -460,6 +462,30 @@ map:
460462
multi << ['reactor.core.publisher.Flux']
461463
}
462464

465+
void "reads global result style mapping #resultStyle" () {
466+
String yaml = """\
467+
openapi-processor-mapping: v2
468+
options: {}
469+
map:
470+
result-style: $resultStyle
471+
"""
472+
473+
when:
474+
def mapping = reader.read (yaml)
475+
def mappings = converter.convert (mapping)
476+
477+
then:
478+
mappings.size () == 1
479+
def type = mappings.first () as OptionMapping
480+
type.name == 'resultStyle'
481+
type.value == expected
482+
483+
where:
484+
resultStyle | expected
485+
'all' | ResultStyle.ALL
486+
'success' | ResultStyle.SUCCESS
487+
}
488+
463489
void "does not fail on 'empty' options: key" () {
464490
String yaml = """\
465491
openapi-processor-mapping: v2

0 commit comments

Comments
 (0)