Skip to content

Extend template models with native oneOf/anyOf/allOf support and discriminator CodeType mapping#3

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/extend-openapi-template-models
Draft

Extend template models with native oneOf/anyOf/allOf support and discriminator CodeType mapping#3
Copilot wants to merge 3 commits intomainfrom
copilot/extend-openapi-template-models

Conversation

Copy link

Copilot AI commented Feb 25, 2026

The template models had no structural representation of OpenAPI polymorphic schemas (oneOf/anyOf/allOf). The workaround was a simplify-polymorphic-schemas patch that flattened everything into a single object, discarding discriminator info and making it impossible to generate correct language-level union/inheritance constructs (e.g. Jackson @JsonSubTypes).

Model changes

DiscriminatorModel (new type)

  • PropertyName string — the JSON property carrying the type tag
  • Mapping map[string]CodeType — discriminator value → fully resolved CodeType (name, qualified type, import path, etc.), giving templates access to all type metadata rather than a bare class name string

Model

  • OneOf/AnyOf/AllOf []CodeType — resolved variant/parent type references (replaces unused []Model)
  • IsOneOf / IsAnyOf / IsAllOf bool — flags for template branching
  • Discriminator *DiscriminatorModel — populated when a discriminator is present

Property

  • Added Required, ReadOnly, WriteOnly bool from the source schema

BuildComponentModels rewrite

Each schema now follows a dedicated branch instead of collapsing into a flat merge:

  • oneOf / anyOf — each *SchemaProxy resolved to a CodeType; discriminator mapping resolved to CodeType values via the component schema registry
  • allOf — referenced sub-schemas → AllOf []CodeType parents; inline sub-schemas → properties merged directly (dedup-aware, inheriting required from both the root schema and the inline sub-schema)
  • object / array / fallback — unchanged, only reached when no polymorphic case applies

Extracted helpers: buildModelProperties (sets Required/ReadOnly/WriteOnly, handles deduplication across allOf merges) and buildDiscriminatorModel (resolves $ref mapping entries to full CodeType values).

Default patches

Removed simplify-polymorphic-schemas from DefaultCodeGenerationPatches — it is no longer needed as a default; it remains available for opt-in use.

Example — discriminator-based oneOf

Pet:
  oneOf:
    - $ref: '#/components/schemas/Cat'
    - $ref: '#/components/schemas/Dog'
  discriminator:
    propertyName: petType
    mapping:
      cat: '#/components/schemas/Cat'
      dog: '#/components/schemas/Dog'

Produces a PetDto model with:

IsOneOf:       true
OneOf:         [CodeType{Name:"CatDto"}, CodeType{Name:"DogDto"}]
Discriminator: {
  PropertyName: "petType",
  Mapping: {
    "cat": CodeType{Name:"CatDto", QualifiedType:"CatDto", ...},
    "dog": CodeType{Name:"DogDto", QualifiedType:"DogDto", ...},
  }
}

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…ve simplify-polymorphic-schemas from defaults

Co-authored-by: PhilippHeuer <10275049+PhilippHeuer@users.noreply.github.com>
Copilot AI changed the title [WIP] Extend internal template models for dynamic structures Proper oneOf/anyOf/allOf support in template models; remove simplify-polymorphic-schemas workaround Feb 25, 2026
Co-authored-by: PhilippHeuer <10275049+PhilippHeuer@users.noreply.github.com>
Copilot AI changed the title Proper oneOf/anyOf/allOf support in template models; remove simplify-polymorphic-schemas workaround Extend template models with native oneOf/anyOf/allOf support and discriminator CodeType mapping Feb 25, 2026
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.

2 participants