Repository navigation
Conversation
…nyOf wrappers A plain `type: number` member maps to java.math.BigDecimal, which is not a Kotlin primitive, so the kotlinx_serialization oneOf/anyOf wrapper serializer emitted `java.math.BigDecimal.serializer()`. kotlinx.serialization has no such serializer, so the generated code did not compile. The number branch now writes the value as a JSON number via `JsonPrimitive`. Fixes OpenAPITools#25452
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 4 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
The unformatted number (java.math.BigDecimal) branch is now encoded as a JSON number, but its decode branch only ran for non-primitive JSON elements, so a number never matched. A plain Json also has no BigDecimal serializer for decodeFromJsonElement. The branch now accepts a non-string JsonPrimitive and builds the BigDecimal from its content.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description:
With the
kotlingenerator,serializationLibrary=kotlinx_serializationandgenerateOneOfAnyOfWrappers=true, ananyOforoneOfwith an unformattedtype: numberbranch generated a wrapper whoseserialize()did not compile. The branch fell through to the{{^isPrimitiveType}}case and wrotejsonEncoder.encodeSerializableValue(java.math.BigDecimal.serializer(), value.value), andBigDecimalhas noserializer().In
kotlin-client/anyof_class.mustacheandoneof_class.mustachethat case now writesjsonEncoder.encodeJsonElement(JsonPrimitive(value.value))whenisNumberis set. Every other non-primitive branch keeps the line it had. The change is limited totype: numberwithout aformat.I added a test in
KotlinClientCodegenModelTestwith3_0/kotlin/issue25452-anyof-oneof-number.yaml, the spec from the issue. It checks the generatedAnyOfValue.ktandOneOfValue.ktand fails without the change.samples/has no diff.Noticed while here: the
{{#isNumber}}{{#isDouble}}and{{#isFloat}}branches in these two templates are never reached, becauseisNumberis not set forformat: doubleorfloat. ADoubleorFloatmember (also viatypeMappings) therefore gets no case inserialize(). This is not addressed in this PR.Fixes #25452
PR checklist
What I ran on this branch (macOS, JDK 21, Maven wrapper):
./mvnw -pl modules/openapi-generator -am test -Dtest=KotlinClientCodegenModelTestpasses (70 tests). With the two templates put back to master, the new test fails../mvnw -pl modules/openapi-generator -am test -Dtest=KotlinClientCodegenModelTest,KotlinClientCodegenApiTest,KotlinModelCodegenTest,KotlinJvmVolleyModelCodegenTestpasses (127 tests, 0 failures, 4 skipped).I generated a
jvm-retrofit2client from the issue spec with the options above and built it with its own Gradle wrapper (./gradlew compileKotlin). With master templates it fails withUnresolved reference 'serializer'inAnyOfValue.ktandOneOfValue.kt. With this change,BUILD SUCCESSFUL../bin/generate-samples.shwith the fourkotlin-oneOf-*-kotlinx-serialization*configs exits 0 and leavessamples/unchanged.I did not run the full
./mvnw clean package, the other sample configs or./bin/utils/export_docs_generators.sh, so the second box is unchecked. No option or docs text changed.cc @karismann @Zomzog @andrewemery @4brunu @yutaka0m @stefankoppier @e5l
Summary by cubic
Fixes the Kotlin client code generator for
oneOf/anyOfwrappers when a branch is an unformattedtype: number, so the generatedkotlinx_serializationcode compiles instead of failing with an unresolvableBigDecimal.serializer()reference.anyof_class.mustacheandoneof_class.mustachenow writes the value as a JSON number viaJsonPrimitiveand deserializes it from a non-stringJsonPrimitivecontent asBigDecimal.BigDecimal.serializer()anddecodeFromJsonElement<java.math.BigDecimal>.Written for commit 86d21ec. Summary will update on new commits.