Skip to content

fix: Apply discriminator literals to allOf subtypes without field reference#101

Open
staging-devin-ai-integration[bot] wants to merge 1 commit intomainfrom
evalon/datamodel--5b9034ed
Open

fix: Apply discriminator literals to allOf subtypes without field reference#101
staging-devin-ai-integration[bot] wants to merge 1 commit intomainfrom
evalon/datamodel--5b9034ed

Conversation

@staging-devin-ai-integration
Copy link

@staging-devin-ai-integration staging-devin-ai-integration bot commented Feb 26, 2026

Summary

Fixes koxudaxi#2528 - OpenAPI polymorphism with discriminator and allOf not working

When a schema has a discriminator and subtypes inherit from it via allOf, the discriminator literal types (e.g., Literal['cat']) should be applied to the subtypes even when the parent schema isn't referenced by any field.

This follows the OpenAPI 3.1 spec example where Pet has a discriminator and Cat/Dog/Lizard extend it via allOf.

Problem

Previously, discriminator literal types were only applied when:

  1. A field referenced the parent schema (e.g., PetContainer.pet: Pet)
  2. This triggered the __apply_discriminator_type processing

But when schemas only use allOf inheritance without a containing field (the example from the spec), the discriminator literals were not applied.

Input Schema:

components:
  schemas:
    Pet:
      type: object
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
        mapping:
          cat: '#/components/schemas/Cat'
          dog: '#/components/schemas/Dog'
    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            name:
              type: string

Before (incorrect):

class Cat(Pet):
    name: str | None = None

After (correct):

class Cat(Pet):
    name: str | None = None
    pet_type: Literal['cat'] = Field(..., alias='petType')

Solution

Added a new method _apply_allof_discriminator_to_models in OpenAPIParser that:

  1. Tracks discriminator subtypes during schema collection in _collect_discriminator_schemas
  2. Stores pending discriminator info mapping subtype refs to (discriminator_value, property_name)
  3. Applies discriminator literal types to subtype models during module processing via override of _process_single_module
  4. Handles field name transformations (e.g., snake_case) and aliases correctly
  5. Supports both single alias and validation_aliases (multiple aliases) patterns

The fix leverages the existing _discriminator_schemas and _discriminator_subtypes dictionaries which were already being populated but not fully utilized.

Testing

Added test case test_main_openapi_discriminator_allof_standalone with input schema and expected output matching the OpenAPI 3.1 spec example.

All 29 existing discriminator-related tests continue to pass.

Notes

…erence

When a schema has a discriminator and subtypes inherit from it via allOf,
the discriminator literal types (e.g., Literal['cat']) should be applied
to the subtypes even when the parent schema isn't referenced by any field.

This follows the OpenAPI 3.1 spec example where Pet has a discriminator
and Cat/Dog/Lizard extend it via allOf. Previously, the discriminator
literal types were only applied when a field referenced the parent schema.

Fixes koxudaxi#2528
@staging-devin-ai-integration staging-devin-ai-integration bot changed the title Fix OpenAPI polymorphism with discriminator and allOf not working (#2528) fix: Apply discriminator literals to allOf subtypes without field reference Feb 26, 2026
@staging-devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OpenAPI polymorphism with discriminator and allOf not working

0 participants