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

Commit b802232

Browse files
committed
#45, parse "null" init value
1 parent 08c5445 commit b802232

File tree

3 files changed

+85
-4
lines changed

3 files changed

+85
-4
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2020 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+
* Used with [io.openapiprocessor.core.converter.ApiOptions.typeMappings] to map an OpenAPI nullable
10+
* property to a wrapper type of the plain type. E.g. org.openapitools.jackson.nullable.JsonNullable
11+
*/
12+
class NullTypeMapping(
13+
14+
/**
15+
* The OpenAPI schema type that should be mapped to the {@link #targetTypeName} java type.
16+
*/
17+
val sourceTypeName: String = "null",
18+
19+
/**
20+
* The fully qualified java type name that will be used as the result type.
21+
*/
22+
val targetTypeName: String,
23+
24+
/**
25+
* An "undefined" value that should be used as the initial value of the property. e.g.
26+
* JonNullable.undefined()
27+
*/
28+
val undefined: String? = null
29+
30+
): Mapping, TargetTypeMapping {
31+
32+
/**
33+
* Returns the target type of this type mapping.
34+
*
35+
* @return the target type
36+
*/
37+
override fun getTargetType (): TargetType {
38+
return TargetType(targetTypeName, emptyList())
39+
}
40+
41+
override fun getChildMappings(): List<Mapping> {
42+
return listOf(this)
43+
}
44+
45+
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class MappingConverter(val mapping: MappingV2) {
5555
}
5656

5757
if(mapping.map.`null` != null) {
58-
result.add(convertType("null", mapping.map.`null`))
58+
result.add(convertNull(mapping.map.`null`))
5959
}
6060

6161
mapping.map.types.forEach {
@@ -81,6 +81,19 @@ class MappingConverter(val mapping: MappingV2) {
8181
return ResultTypeMapping(result)
8282
}
8383

84+
private fun convertNull(value: String): Mapping {
85+
val split = value
86+
.split(" = ")
87+
.map { it.trim() }
88+
89+
val type = split.component1()
90+
var init: String? = null
91+
if (split.size == 2)
92+
init = split.component2()
93+
94+
return NullTypeMapping("null", type, init)
95+
}
96+
8497
private fun convertType (from: String, to: String): Mapping {
8598
return TypeMapping(from, to)
8699
}
@@ -144,7 +157,7 @@ class MappingConverter(val mapping: MappingV2) {
144157
}
145158

146159
if(source.`null` != null) {
147-
result.add(convertType("null", source.`null`))
160+
result.add(convertNull(source.`null`))
148161
}
149162

150163
source.types.forEach {

src/test/kotlin/io/openapiprocessor/core/processor/mapping/v2/MappingConverterSpec.kt

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package io.openapiprocessor.core.processor.mapping.v2
88
import io.kotest.core.spec.IsolationMode
99
import io.kotest.core.spec.style.StringSpec
1010
import io.kotest.matchers.shouldBe
11+
import io.openapiprocessor.core.converter.mapping.NullTypeMapping
1112
import io.openapiprocessor.core.converter.mapping.TypeMapping
1213
import io.openapiprocessor.core.processor.MappingConverter
1314
import io.openapiprocessor.core.processor.MappingReader
@@ -56,7 +57,7 @@ class MappingConverterSpec: StringSpec({
5657
val mappings = converter.convert (mapping)
5758

5859
// then:
59-
val `null` = mappings.first() as TypeMapping
60+
val `null` = mappings.first() as NullTypeMapping
6061
`null`.targetTypeName shouldBe "org.openapitools.jackson.nullable.JsonNullable"
6162
}
6263

@@ -78,7 +79,29 @@ class MappingConverterSpec: StringSpec({
7879
val mappings = converter.convert (mapping)
7980

8081
// then:
81-
val `null` = mappings.first().getChildMappings().first() as TypeMapping
82+
val `null` = mappings.first().getChildMappings().first() as NullTypeMapping
83+
`null`.targetTypeName shouldBe "org.openapitools.jackson.nullable.JsonNullable"
84+
}
85+
86+
"read endpoint 'null' mapping with init value" {
87+
val yaml = """
88+
|openapi-processor-mapping: v2
89+
|
90+
|options:
91+
| package-name: io.openapiprocessor.somewhere
92+
|
93+
|map:
94+
| paths:
95+
| /foo:
96+
| null: org.openapitools.jackson.nullable.JsonNullable = JsonNullable.undefined()
97+
""".trimMargin()
98+
99+
// when:
100+
val mapping = reader.read (yaml)
101+
val mappings = converter.convert (mapping)
102+
103+
// then:
104+
val `null` = mappings.first().getChildMappings().first() as NullTypeMapping
82105
`null`.targetTypeName shouldBe "org.openapitools.jackson.nullable.JsonNullable"
83106
}
84107

0 commit comments

Comments
 (0)