You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(kotlin): support non-discriminator oneOf/anyOf model and array types with kotlinx_serialization (#23007)
* fix(kotlin): use sealed interface for non-discriminator oneOf/anyOf in kotlinx_serialization
* add sample and CI config for non-discriminator oneOf/anyOf with kotlinx_serialization
* replace petstore with focused spec for non-discriminator oneOf/anyOf sample
* add CI path filter for kotlin-oneOf-anyOf-kotlinx-serialization sample
* add x-duplicated-data-type guard to kotlinx_serialization oneOf/anyOf templates
The Gson path already uses {{^vendorExtensions.x-duplicated-data-type}} to skip
duplicate data types, but the new kotlinx_serialization path was missing this
guard. Without it, duplicate value class names would be generated if multiple
schemas resolve to the same Kotlin dataType, causing compilation errors.
* remove invalid path pattern from push.branches in CI config
push.branches filters by branch name, not file paths. The sample directory
pattern added here had no effect. The pull_request.paths filter remains and
correctly triggers CI for this sample.
* update generateOneOfAnyOfWrappers docs to include kotlinx_serialization support
Copy file name to clipboardExpand all lines: docs/generators/kotlin.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
27
27
|enumPropertyNaming|Naming convention for enum properties: 'camelCase', 'PascalCase', 'snake_case', 'UPPERCASE', and 'original'||original|
28
28
|explicitApi|Generates code with explicit access modifiers to comply with Kotlin Explicit API Mode.||false|
29
29
|failOnUnknownProperties|Fail Jackson de-serialization on unknown properties||false|
30
-
|generateOneOfAnyOfWrappers|Generate oneOf, anyOf schemas as wrappers. Only `jvm-retrofit2`(library), `gson`(serializationLibrary) support this option.||false|
30
+
|generateOneOfAnyOfWrappers|Generate oneOf, anyOf schemas as wrappers. Only `jvm-retrofit2`(library) with `gson` or `kotlinx_serialization`(serializationLibrary) support this option.||false|
31
31
|generateRoomModels|Generate Android Room database models in addition to API models (JVM Volley library only)||false|
Copy file name to clipboardExpand all lines: modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java
+24-1Lines changed: 24 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -286,7 +286,7 @@ public KotlinClientCodegen() {
286
286
287
287
cliOptions.add(newCliOption(MAP_FILE_BINARY_TO_BYTE_ARRAY, "Map File and Binary to ByteArray (default: false)").defaultValue(Boolean.FALSE.toString()));
288
288
289
-
cliOptions.add(CliOption.newBoolean(GENERATE_ONEOF_ANYOF_WRAPPERS, "Generate oneOf, anyOf schemas as wrappers. Only `jvm-retrofit2`(library), `gson`(serializationLibrary) support this option."));
289
+
cliOptions.add(CliOption.newBoolean(GENERATE_ONEOF_ANYOF_WRAPPERS, "Generate oneOf, anyOf schemas as wrappers. Only `jvm-retrofit2`(library) with `gson` or `kotlinx_serialization`(serializationLibrary) support this option."));
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("{{classname}}")
119
+
120
+
override fun serialize(encoder: Encoder, value: {{classname}}) {
121
+
val jsonEncoder = encoder as? JsonEncoder ?: throw SerializationException("{{classname}} can only be serialized with Json")
122
+
123
+
when (value) {
124
+
{{#composedSchemas}}
125
+
{{#anyOf}}
126
+
{{^vendorExtensions.x-duplicated-data-type}}
127
+
{{#isArray}}
128
+
is {{classname}}.{{#fnToValueClassName}}{{{dataType}}}{{/fnToValueClassName}} -> jsonEncoder.encodeJsonElement(jsonEncoder.json.encodeToJsonElement(value.value))
129
+
{{/isArray}}
130
+
{{^isArray}}
131
+
{{#isPrimitiveType}}
132
+
{{#isString}}
133
+
is {{classname}}.{{#fnToValueClassName}}{{{dataType}}}{{/fnToValueClassName}} -> jsonEncoder.encodeString(value.value)
134
+
{{/isString}}
135
+
{{#isBoolean}}
136
+
is {{classname}}.{{#fnToValueClassName}}{{{dataType}}}{{/fnToValueClassName}} -> jsonEncoder.encodeBoolean(value.value)
137
+
{{/isBoolean}}
138
+
{{#isInteger}}
139
+
{{^isLong}}
140
+
is {{classname}}.{{#fnToValueClassName}}{{{dataType}}}{{/fnToValueClassName}} -> jsonEncoder.encodeInt(value.value)
141
+
{{/isLong}}
142
+
{{/isInteger}}
143
+
{{#isLong}}
144
+
is {{classname}}.{{#fnToValueClassName}}{{{dataType}}}{{/fnToValueClassName}} -> jsonEncoder.encodeLong(value.value)
145
+
{{/isLong}}
146
+
{{#isNumber}}
147
+
{{#isDouble}}
148
+
is {{classname}}.{{#fnToValueClassName}}{{{dataType}}}{{/fnToValueClassName}} -> jsonEncoder.encodeDouble(value.value)
149
+
{{/isDouble}}
150
+
{{#isFloat}}
151
+
is {{classname}}.{{#fnToValueClassName}}{{{dataType}}}{{/fnToValueClassName}} -> jsonEncoder.encodeFloat(value.value)
152
+
{{/isFloat}}
153
+
{{/isNumber}}
154
+
{{/isPrimitiveType}}
155
+
{{^isPrimitiveType}}
156
+
is {{classname}}.{{#fnToValueClassName}}{{{dataType}}}{{/fnToValueClassName}} -> jsonEncoder.encodeSerializableValue({{{dataType}}}.serializer(), value.value)
157
+
{{/isPrimitiveType}}
158
+
{{/isArray}}
159
+
{{/vendorExtensions.x-duplicated-data-type}}
160
+
{{/anyOf}}
161
+
{{/composedSchemas}}
162
+
}
163
+
}
164
+
165
+
override fun deserialize(decoder: Decoder): {{classname}} {
166
+
val jsonDecoder = decoder as? JsonDecoder ?: throw SerializationException("{{classname}} can only be deserialized with Json")
167
+
val jsonElement = jsonDecoder.decodeJsonElement()
168
+
169
+
val errorMessages = mutableListOf<String>()
170
+
171
+
{{#composedSchemas}}
172
+
{{#anyOf}}
173
+
{{^vendorExtensions.x-duplicated-data-type}}
174
+
{{#isArray}}
175
+
if (jsonElement is JsonArray) {
176
+
try {
177
+
val instance = jsonDecoder.json.decodeFromJsonElement<{{{dataType}}}>(jsonElement)
0 commit comments