← 输入框|github.com|刷新
Skip to content

[Feature]: Make ambiguous oneOf violations configurable #409

Closed

Description

@visheshupadhyay

OpenAPI oneOf requires a payload to match exactly one schema. When a payload matches more than one schema, the OpenAPI validator reports an ambiguity violation such as:

Instance failed to match exactly one schema (matched 2 out of 4)

InternalViolationExclusions currently suppresses these violations unconditionally. As a result, applications cannot choose to log or report them, even when ambiguous API contracts are important to detect.

This change preserves the existing default behaviour while allowing callers to choose whether these violations should be ignored.

Why this is useful

Ignoring ambiguous oneOf violations can be a sensible default because some specifications have intentionally overlapping schemas. However, another application may want to detect these cases because they can reveal an unclear API contract or an unexpected response shape.

The library should support both choices without changing behaviour for existing users.

Proposed behaviour

  • Existing one-argument constructor keeps the current behaviour: ambiguous oneOf violations are ignored.
  • The new constructor accepts ignoreAmbiguousOneOfViolations.
    • true: ignore the violation.
    • false: do not exclude it, allowing it to reach the normal reporting flow.

Implementation

InternalViolationExclusions now has explicit constructors instead of Lombok's generated all-arguments constructor:

public InternalViolationExclusions(ViolationExclusions customViolationExclusions) {
    this(customViolationExclusions, true);
}

public InternalViolationExclusions(
        ViolationExclusions customViolationExclusions,
        boolean ignoreAmbiguousOneOfViolations) {
    this.customViolationExclusions = customViolationExclusions;
    this.ignoreAmbiguousOneOfViolations = ignoreAmbiguousOneOfViolations;
}

The oneOf exclusion is now applied only when ignoreAmbiguousOneOfViolations is true.

Tests

The tests cover all compatibility and configuration paths:

  • Existing one-argument constructor still excludes ambiguous oneOf violations.
  • New constructor with true excludes them.
  • New constructor with false does not exclude them.

Validation command:

.\gradlew.bat :openapi-validation-core:test --tests "com.getyourguide.openapi.validation.core.exclusions.InternalViolationExclusionsTest"

Backward compatibility

This is backward compatible. Existing callers keep using the one-argument constructor and receive the same behaviour as before.

Test runs and build run:

Build run before changes:

PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi> .\gradlew.bat clean test
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

> Task :examples:example-spring-boot-starter-web:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-web/src/main/resources/openapi.yaml
Spec is valid.

> Task :examples:example-spring-boot-starter-web:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator.                                                      #
# We appreciate your support! Please consider donation to help us maintain this project.   #
# https://opencollective.com/openapi_generator/donate                                      #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-web\build/generated

> Task :examples:example-spring-boot-starter-web:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

> Task :examples:example-spring-boot-starter-webflux:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-webflux/src/main/resources/openapi.yaml
Spec is valid.

> Task :examples:example-spring-boot-starter-webflux:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator.                                                      #
# We appreciate your support! Please consider donation to help us maintain this project.   #
# https://opencollective.com/openapi_generator/donate                                      #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-webflux\build/generated

> Task :spring-boot-starter:spring-boot-starter-webflux:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

> Task :examples:example-spring-boot-starter-webflux:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

> Task :test:openapi-web:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-web/../openapi.yaml
Spec is valid.

> Task :test:openapi-web:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator.                                                      #
# We appreciate your support! Please consider donation to help us maintain this project.   #
# https://opencollective.com/openapi_generator/donate                                      #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-web\build/generated/openapi

> Task :test:openapi-web:compileTestFixturesJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

> Task :test:openapi-webflux:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-webflux/../openapi.yaml
Spec is valid.

> Task :test:openapi-webflux:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator.                                                      #
# We appreciate your support! Please consider donation to help us maintain this project.   #
# https://opencollective.com/openapi_generator/donate                                      #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-webflux\build/generated/openapi

> Task :test:openapi-webflux:compileTestFixturesJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

[Incubating] Problems report is available at: file:///C:/Users/vishe/OneDrive/Desktop/Java-Backend/OpenSource/GetYourGuide/openapi/build/reports/problems/problems-report.html

Deprecated Gradle features were used in this build, making it incompatible with Gradle 10.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/9.4.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD SUCCESSFUL in 1m 19s
74 actionable tasks: 59 executed, 15 up-to-date
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.4.1/userguide/configuration_cache_enabling.html
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi>

Test Run without any change in production files or test file:

PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi> .\gradlew.bat :openapi-validation-core:test --tests "com.getyourguide.openapi.validation.core.exclusions.InternalViolationExclusionsTest"
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

[Incubating] Problems report is available at: file:///C:/Users/vishe/OneDrive/Desktop/Java-Backend/OpenSource/GetYourGuide/openapi/build/reports/problems/problems-report.html

Deprecated Gradle features were used in this build, making it incompatible with Gradle 10.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/9.4.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD SUCCESSFUL in 11s
5 actionable tasks: 1 executed, 4 up-to-date
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.4.1/userguide/configuration_cache_enabling.html
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi>

Test Run after adding a scneario where existing code will fail:

PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi> .\gradlew.bat :openapi-validation-core:test --tests "com.getyourguide.openapi.validation.core.exclusions.InternalViolationExclusionsTest"

> Task :openapi-validation-core:compileTestJava FAILED
C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\openapi-validation-core\src\test\java\com\getyourguide\openapi\validation\core\exclusions\InternalViolationExclusionsTest.java:73: error: constructor InternalViolationExclusions in class InternalViolationExclusions cannot be applied to given types;
        violationExclusions = new InternalViolationExclusions(customViolationExclusions, false);
                              ^
  required: ViolationExclusions
  found:    ViolationExclusions,boolean
  reason: actual and formal argument lists differ in length
1 error

[Incubating] Problems report is available at: file:///C:/Users/vishe/OneDrive/Desktop/Java-Backend/OpenSource/GetYourGuide/openapi/build/reports/problems/problems-report.html

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':openapi-validation-core:compileTestJava'.
> Compilation failed; see the compiler output below.
  C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\openapi-validation-core\src\test\java\com\getyourguide\openapi\validation\core\exclusions\InternalViolationExclusionsTest.java:73: error: constructor InternalViolationExclusions in class InternalViolationExclusions cannot be applied to given types;
          violationExclusions = new InternalViolationExclusions(customViolationExclusions, false);
                                ^
    required: ViolationExclusions
    found:    ViolationExclusions,boolean
    reason: actual and formal argument lists differ in length
  1 error

* Try:
> Check your code and dependencies to fix the compilation error(s)
> Run with --scan to get full insights from a Build Scan (powered by Develocity).

Deprecated Gradle features were used in this build, making it incompatible with Gradle 10.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/9.4.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD FAILED in 5s
4 actionable tasks: 1 executed, 3 up-to-date
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi>

Test run after applying the fix:

PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi> .\gradlew.bat :openapi-validation-core:test --tests "com.getyourguide.openapi.validation.core.exclusions.InternalViolationExclusionsTest"
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

[Incubating] Problems report is available at: file:///C:/Users/vishe/OneDrive/Desktop/Java-Backend/OpenSource/GetYourGuide/openapi/build/reports/problems/problems-report.html

Deprecated Gradle features were used in this build, making it incompatible with Gradle 10.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/9.4.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD SUCCESSFUL in 11s
5 actionable tasks: 2 executed, 3 up-to-date
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.4.1/userguide/configuration_cache_enabling.html
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi>

Build run after adding the changes:

PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi> .\gradlew.bat clean test
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

> Task :examples:example-spring-boot-starter-web:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-web/src/main/resources/openapi.yaml
Spec is valid.

> Task :examples:example-spring-boot-starter-web:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator.                                                      #
# We appreciate your support! Please consider donation to help us maintain this project.   #
# https://opencollective.com/openapi_generator/donate                                      #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-web\build/generated

> Task :examples:example-spring-boot-starter-web:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

> Task :examples:example-spring-boot-starter-webflux:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-webflux/src/main/resources/openapi.yaml
Spec is valid.

> Task :examples:example-spring-boot-starter-webflux:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator.                                                      #
# We appreciate your support! Please consider donation to help us maintain this project.   #
# https://opencollective.com/openapi_generator/donate                                      #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\examples\example-spring-boot-starter-webflux\build/generated

> Task :spring-boot-starter:spring-boot-starter-webflux:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

> Task :examples:example-spring-boot-starter-webflux:compileJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

> Task :test:openapi-web:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-web/../openapi.yaml
Spec is valid.

> Task :test:openapi-web:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator.                                                      #
# We appreciate your support! Please consider donation to help us maintain this project.   #
# https://opencollective.com/openapi_generator/donate                                      #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-web\build/generated/openapi

> Task :test:openapi-web:compileTestFixturesJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

> Task :test:openapi-webflux:openApiValidate
Validating spec C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-webflux/../openapi.yaml
Spec is valid.

> Task :test:openapi-webflux:openApiGenerate
############################################################################################
# Thanks for using OpenAPI Generator.                                                      #
# We appreciate your support! Please consider donation to help us maintain this project.   #
# https://opencollective.com/openapi_generator/donate                                      #
############################################################################################
Successfully generated code to C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi\test\openapi-webflux\build/generated/openapi

> Task :test:openapi-webflux:compileTestFixturesJava
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Java HotSpot(TM) 64-Bit Server VM warning: Sharing is only supported for boot loader classes because bootstrap classpath has been appended

[Incubating] Problems report is available at: file:///C:/Users/vishe/OneDrive/Desktop/Java-Backend/OpenSource/GetYourGuide/openapi/build/reports/problems/problems-report.html

Deprecated Gradle features were used in this build, making it incompatible with Gradle 10.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

For more on this, please refer to https://docs.gradle.org/9.4.1/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.

BUILD SUCCESSFUL in 2m 8s
74 actionable tasks: 70 executed, 4 up-to-date
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.4.1/userguide/configuration_cache_enabling.html
PS C:\Users\vishe\OneDrive\Desktop\Java-Backend\OpenSource\GetYourGuide\openapi>

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions