Skip to content

Conversation

@mike-taylor99
Copy link
Contributor

@mike-taylor99 mike-taylor99 commented Nov 19, 2025

Summary

Adds support for discriminator properties in JSON Schema emitter with an opt-in oneOf/anyOf union generation option.

Changes

  • Automatically injects discriminator property into base models with @discriminator decorator
  • Marks discriminator property as required in generated schemas
  • Adds new polymorphic-models-strategy emitter option to generate oneOf or anyOf schemas for closed discriminated unions
  • Includes discriminator.mapping for improved validation

Testing

  • Added 11 comprehensive tests covering:
    • Basic discriminator property injection
    • Required field marking
    • OneOf union generation with mapping
    • Edge cases and validation

Documentation

All tests passing. Changeset included for release notes.

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 19, 2025

Open in StackBlitz

npm i https://pkg.pr.new/microsoft/typespec/@typespec/json-schema@9038

commit: 1dc719a

@github-actions
Copy link
Contributor

github-actions bot commented Nov 19, 2025

All changed packages have been documented.

  • @typespec/json-schema
Show changes

@typespec/json-schema - feature ✏️

Add discriminator support and polymorphic models strategy option,> ,> - Automatically injects discriminator property into base models with @discriminator decorator,> - Marks discriminator property as required in generated schemas,> - New polymorphic-models-strategy emitter option with three strategies:,> - ignore: Emit as regular object schema (default),> - oneOf: Emit oneOf schema for closed discriminated unions,> - anyOf: Emit anyOf schema for open discriminated unions,> - Includes discriminator.mapping in oneOf/anyOf schemas for improved validation

@timotheeguerin
Copy link
Member

/azp run typespec - pr tools

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Member

@timotheeguerin timotheeguerin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just nit on the test name

@timotheeguerin
Copy link
Member

/azp run typespec - pr tools

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@azure-sdk
Copy link
Collaborator

azure-sdk commented Nov 19, 2025

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@mike-taylor99 mike-taylor99 changed the title feat(json-schema): Add discriminator support and oneOf union option feat(json-schema): Add discriminator support and oneOf/anyOf union option Nov 19, 2025
@timotheeguerin
Copy link
Member

/azp run typespec - pr tools

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@timotheeguerin timotheeguerin added this pull request to the merge queue Nov 20, 2025
Merged via the queue into microsoft:main with commit 1c596aa Nov 20, 2025
27 checks passed
@mike-taylor99 mike-taylor99 deleted the mictaylor/json-schema-discriminator-support branch November 20, 2025 19:24
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.

3 participants