Skip to content

feat: add PoC of handling discriminator for oneOf/anyOf cases in OpenAPI 3.1 #4952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

glowcloud
Copy link
Contributor

@glowcloud glowcloud commented Jun 12, 2025

Notes on the idea of handling discriminator:

Dereference: dereference value of MemberElement contained within Discriminator.mapping and set the result as its metadata.

discriminator:
   propertyName: petType
   mapping:
      dog: Dog # => should be dereferenced and the dereference result should be set as metadata

Normalization: compare oneOf/anyOf items with the mapping values metadata and create normalized mapping based on the result:

  • If both oneOf and anyOf are present, do not normalize
  • Compare external references and internal references that do not point to components/schema/<schemaName>:
    • These references will be recognized by having a different baseURI or by not having schemaName metadata.
    • The comparison is done based on $refBaseURI metadata.
    • If no mapping exists, normalization will not be successful.
  • Compare internal references that point to components/schema/<schemaName>:
    • These references will be recognized by having schemaName metadata.
    • The comparison is done based on schemaName and $refBaseURI metadata.
    • If no mapping exists, create it as <schemaName>: <schema>.
  • If normalization was successful, set the normalized mapping to a Specification Extension field (e.g. x-normalized-mapping)

Additionally, we should have a new strategy option to check if we should dereference Discriminator.mapping values.

@glowcloud glowcloud requested a review from char0n June 12, 2025 09:32
@glowcloud glowcloud self-assigned this Jun 12, 2025
@glowcloud glowcloud added enhancement New feature or request OpenAPI 3.1 labels Jun 12, 2025

const schemaJSONPointer = ancestorLineageToJSONPointer([
...ancestors,
parent!,

Choose a reason for hiding this comment

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

won't passing an undefined parent cause a type error?

const mapping =
schemaElement.discriminator.get('mapping') ?? new DiscriminatorMappingElement();
const normalizedMapping: DiscriminatorMappingElement = cloneDeep(mapping);
let normalizationResult = true;

Choose a reason for hiding this comment

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

Suggested change
let normalizationResult = true;
let hasNormalizationResult = true;

]);

// skip visiting this Schema Object if it's already normalized
if (storage!.includes(schemaJSONPointer)) {

Choose a reason for hiding this comment

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

Maybe we should check before whether storage is not being empty?

Comment on lines +95 to +102
const items = isArrayElement(schemaElement.oneOf)
? schemaElement.oneOf
: schemaElement.anyOf;

items!.forEach((item) => {
if (!isSchemaElement(item)) {
return;
}

Choose a reason for hiding this comment

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

Suggested change
const items = isArrayElement(schemaElement.oneOf)
? schemaElement.oneOf
: schemaElement.anyOf;
items!.forEach((item) => {
if (!isSchemaElement(item)) {
return;
}
const items = isArrayElement(schemaElement.oneOf)
? schemaElement.oneOf
: (schemaElement.anyOf as ArrayElement);
items.forEach((item) => {
if (!isSchemaElement(item)) {
return;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request OpenAPI 3.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants