Repository navigation
Conversation
Adds the validateModelDiscriminator option (default: true): a schema with a oneOf or anyOf composition should define a discriminator so that API consumers can tell the possible object types apart. allOf is out of scope as it describes a merged model rather than a set of alternatives. Fixes OpenAPITools#806
There was a problem hiding this comment.
3 issues found across 16 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java">
<violation number="1" location="lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java:226">
P2: The new tests never verify that empty `oneOf` or `anyOf` lists are ignored. Add empty-list fixtures and assert zero errors to cover the rule’s explicit non-empty condition.</violation>
<violation number="2" location="lib/src/test/java/org/openapitools/openapistylevalidator/OpenApiSpecStyleValidatorTest.java:226">
P2: These fixtures do not test the documented inline-composition exclusion. Add an inline schema containing `oneOf` or `anyOf` and assert that validation reports no discriminator error.</violation>
</file>
<file name="README.md">
<violation number="1" location="README.md:119">
P3: This description overstates the rule: validation only covers named schemas in `components/schemas` with a non-empty `oneOf` or `anyOf`, not inline compositions. Clarify that scope so users do not expect inline schemas to be checked.</violation>
</file>
Reply with feedback, questions, or to request a fix.
View guided diff | Re-trigger cubic
…r rule - add fixtures asserting that an empty oneOf or anyOf list does not trigger the missing-discriminator error (an empty composition list declares nothing to discriminate) - add a fixture asserting that a composition declared inline, inside a property of a named schema, is not reported: the rule is deliberately scoped to the named schemas of components/schemas - qualify the README options-table row and the Models section accordingly
This branch has not been deployed
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.
What
Adds a new style rule for the concern raised in #806: a schema that composes alternative models with
oneOforanyOfshould define adiscriminator, so that API consumers can tell the possible object types apart (see the Swagger documentation on inheritance and polymorphism referenced in the issue).Rule semantics
components/schemasthat declares a non-emptyoneOforanyOfand has nodiscriminatoris reported:*ERROR* in Model 'MyComposition', field 'discriminator' -> A schema with a oneOf or anyOf composition should define a discriminatordiscriminatorobject, as documented by the Swagger reference (the discriminator lives on the schema that carriesoneOf/anyOf).allOfis out of scope: it describes a merged model rather than a set of alternatives, and the referenced documentation limits the use of the discriminator tooneOfandanyOf.components/schemas, like the existing model rules; covering inline compositions (e.g. in responses) could be a follow-up.Config
New option
validateModelDiscriminator(boolean, defaulttrue), following the repo convention of enabling all rules by default:Tests
testModelDiscriminatorValidation(parameterized):oneOfwithout a discriminator is reported when the rule is on and stays silent when the flag is offtestModelDiscriminatorValidationSuccessWhenDiscriminatorIsDefined: aoneOfschema with adiscriminatorproduces no errortestModelDiscriminatorValidationReportsAnyOfComposition:anyOfcompositions are covered tootestModelDiscriminatorValidationIgnoresAllOfComposition:allOfis not reportedValidatorParametersTestcovers the new default;ErrorAggregatorTestcovers the new error; the CLIdefault.jsonfixture was updated accordingly./gradlew spotlessApply testpasses on all modules (lib, cli, gradle-plugin, maven-plugin).Fixes #806
Summary by cubic
Adds a model validation rule that flags named schemas in
components/schemaswith a non-emptyoneOforanyOfcomposition that don't define adiscriminator, so consumers can tell the possible object types apart.New Features
validateModelDiscriminatoroption (defaults totrue) exposed through the CLI, Gradle, and Maven plugins.components/schemasare checked; inline and empty compositions are not reported.allOfis ignored since it describes a merged model rather than a set of alternatives.Written for commit d030edd. Summary will update on new commits.