[core] Resolve oneOf discriminator type when property is inherited by alternatives#6
Open
Ignacio-Vidal wants to merge 1 commit into
Open
[core] Resolve oneOf discriminator type when property is inherited by alternatives#6Ignacio-Vidal wants to merge 1 commit into
Ignacio-Vidal wants to merge 1 commit into
Conversation
… alternatives When a oneOf/anyOf interface declares the discriminator on itself but does not define the discriminator property locally, the property type was resolved as String instead of the actual type. This happens when each alternative inherits the discriminator property from a shared base via allOf, while the discriminator keyword lives on the oneOf interface schema rather than on the base. recursiveGetDiscriminator now descends into the schema's oneOf/anyOf/allOf alternatives (cycle-guarded) to discover the discriminator property schema when the interface schema does not define it locally, so the generated interface getter uses the concrete type (e.g. an enum) rather than falling back to String. Adds a DefaultCodegenTest case and matching schemas in oneOfDiscriminator.yaml. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Lw5PwrLknPHfsnkwTu2N1B
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.
Problem
When a
oneOf/anyOfinterface declares the discriminator on itself but does not define the discriminator property locally, the discriminator property type was resolved asStringinstead of the actual type.This happens when each alternative inherits the discriminator property from a shared base via
allOf, while thediscriminatorkeyword lives on theoneOfinterface schema rather than on the base.Concrete repro (jaxrs-spec,
useOneOfInterfaces):PetRequestV2declaresproperties.petType: $ref PetTypeinline on the schema → generatedPetType getPetType();✅PetRequestV3has nopropertieson the schema;petTypeis defined in a shared base referenced by each alternative'sallOf→ generatedString getPetType();❌Upstream PR OpenAPITools#24158 fixed the related case where the discriminator lives on the shared base and is inherited by the alternatives, but not this variant where the discriminator sits on the
oneOfinterface itself.Fix
DiscriminatorUtils.recursiveGetDiscriminatornow descends into the schema'soneOf/anyOf/allOfalternatives (cycle-guarded) to discover the discriminator property schema when the interface schema does not define it locally. The generated interface getter then uses the concrete type (e.g. an enum) rather than falling back toString.The change is a strictly additive fallback: it only fires when the previous code returned
nullfor the discriminator schema (which is exactly the case that fell back toString).Tests
testOneOfDiscriminatorOnInterfacePropertyInAlternativesinDefaultCodegenTestasserting the resolved type is the enum (PetType) andgetIsEnum()is true.PetRequestV3,PetV3Base,CatRequestV3,DogRequestV3) inoneOfDiscriminator.yamlmodelling the discriminator-on-interface + property-in-alternatives pattern.Verification
DefaultCodegenTest(171),JavaJAXRSSpecServerCodegenTest(148), plusJavaClientCodegenTest,SpringCodegenTest,JavaHelidonCommonCodegenTest,JavaMicronautClientCodegenTest— all pass, no regressions.PetRequestV3now emitsPetType getPetType();;PetRequestV2unchanged.🤖 Generated with Claude Code
https://claude.ai/code/session_01Lw5PwrLknPHfsnkwTu2N1B