Repository navigation
Conversation
… kotlinx_serialization Inline enums on a model were annotated with a plain @serializable, so kotlinx.serialization wrote integer and number enums by their @SerialName, as the JSON string "1" instead of the number 1. Inline non-string, non-container enums now get the same value serializer as standalone enums when enumUnknownDefaultCase is off. Models with enums import the serializer types, and multiplatform output is unchanged. Fixes OpenAPITools#25459
Contributor
There was a problem hiding this comment.
All reported issues were addressed across 7 files
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
…tlinx_serialization type: number maps to java.math.BigDecimal, which has no serializer() for the generated value serializer to call, so these enums referenced an unresolved symbol. Inline number enums keep the plain @serializable they had before, while integer, long, float and double enums still use the value serializer.
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
serializationLibrary=kotlinx_serialization, a number enum declared inline on a property (plays: { type: integer, enum: [1, 2] }) was written as the JSON string"1"and could not be read from the number1. The same enum declared as its own schema already works, because #21746 gaveenum_class.mustachea value serializer for every non-string enum.data_class.mustacheonly added(with = …Serializer::class)and the serializer object underenumUnknownDefaultCase.kotlin-client/data_class.mustachenow does the same asenum_class.mustachefor inline enums: whenenumUnknownDefaultCaseis off and the enum is not a string or container, the nested enum gets@Serializable(with = <Name>Serializer::class)and aninternal object <Name>Serializerthat reads and writesvaluewith the enum's type serializer. TheenumUnknownDefaultCasepath is unchanged. The neededkotlinx.serializationimports are added when the model has enums. The serializer object is not emitted formultiplatform, which is out of scope here: inline number enums there still behave as before. Number (BigDecimal) enums keep the previous plain @serializable path; supporting them properly needs a toEnumValue change across Kotlin generators, so I left that out of scope.I added two tests in
KotlinClientCodegenModelTestwith3_0/kotlin/issue25459-kotlinx-inline-numeric-enum.yaml, one for@Serializableand one forserializableModel=true(@KSerializable). Both fail without the change. Regenerating the kotlinx samples adds the four imports toPet.ktandOrder.ktinkotlin-jvm-ktor-kotlinx_serializationandkotlin-retrofit2-kotlinx_serialization. Their inline enums are strings, so nothing else in them changes.Fixes #25459
PR checklist
What I ran on this branch (macOS, JDK 21, Maven wrapper):
./mvnw -pl modules/openapi-generator -am test -Dtest=KotlinClientCodegenModelTestpasses (71 tests). Withdata_class.mustacheput back to master, the two new tests fail../mvnw -pl modules/openapi-generator -am test -Dtest=KotlinClientCodegenModelTest,KotlinClientCodegenApiTest,KotlinModelCodegenTestpasses (126 tests, 0 failures, 4 skipped).Both changed samples compile with their own Gradle wrapper (
./gradlew compileKotlin).I generated a
jvm-retrofit2kotlinx client from the issue spec and added a small round-trip test locally (not committed). With this change,Recording(plays = _1, speed = _1)encodes as{"plays":1,"speed":1}and{"plays":2,"speed":2}decodes. With master templates it encodes{"plays":"1","speed":1}and decoding fails withJsonDecodingException. The same test passes withserializableModel=true../bin/generate-samples.shwith the ten configs that setserializationLibrary: kotlinx_serialization, pluskotlin-multiplatform.yamlandkotlin-enum-integers-multiplatform.yaml, exits 0. Only the four files above change.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 inline number enum serialization in Kotlin clients using
kotlinx_serialization. An enum declared inline on a property (enum: [1, 2]) was written as the JSON string"1"and failed to decode from the number1; it now serializes and deserializes by value, matching standalone enums (which already had a value serializer from #21746).enumUnknownDefaultCaseis off; that code path is unchanged.multiplatformoutput is unchanged.kotlinx.serializationimports because their inline enums are strings.Written for commit 310a46e. Summary will update on new commits.