Validate that object schemas declare additionalProperties
What
This adds a new model rule, enabled by the validateModelAdditionalProperties option, that flags named schemas in components/schemas which are objects but do not declare additionalProperties - neither a boolean nor a schema.
A schema counts as an object when it says so explicitly (type: object) or when it declares properties (the type keyword is optional in that case). Schemas of other types, compositions that declare no properties of their own (e.g. an allOf used for inheritance) and empty properties maps are left alone.
The rule is opt-in, false by default: most existing specs do not declare additionalProperties on every object, so flagging them by default would be too noisy for a validator that enables everything else out of the box. Users who want the strictness can turn it on in their options file (CLI), Gradle extension or Maven configuration. A finding looks like:
*ERROR* in Model 'MyObject', field 'additionalProperties' -> An object schema should declare additionalProperties
Why
When additionalProperties is absent, whether an object accepts properties that are not listed in the spec is left to the implementation (see the Schema Object definition). Declaring the keyword makes the contract explicit - that is what #741 asks for.
How
- new
AdditionalPropertiesValidatorinlib, invoked fromvalidateModels()behind the new flag, in the same style as the discriminator rule ModelStyleError.toString()now omits the property part when a finding concerns the schema itself rather than one of its properties (same hardening as #811 - model-level findings have no property name, and the old format printedproperty 'null')- the option is wired through
ValidatorParameters, the CLI options file, the Gradle plugin task and the Maven plugin mojo, with the default documented in the README options table and in the samplespecs/options.json - unit tests cover the flagged case (explicit and inferred objects, with the expected message), the clean cases (keyword declared as
true,falseor as a schema) and the opt-in behavior (no violation with default parameters, none when the rule is disabled)
The wiring follows #809, #810 and #811 as precedent.
One implementation note: the new tests use plain @Test methods on purpose. junit-jupiter-params is declared testCompileOnly in the root build.gradle, so @ParameterizedTest methods compile but are silently skipped at runtime (this affects the existing parameterized tests too - happy to see that fixed in a separate change).
The full suite (./gradlew build, including the Gradle plugin functional tests and the Maven plugin integration test) is green.
Fixes #741
Summary by cubic
Adds an opt-in model rule that flags named object schemas in components/schemas which do not declare additionalProperties. The rule is off by default (validateModelAdditionalProperties: false) because most existing specs omit the keyword; when the keyword is absent, whether an object accepts properties outside the spec is left to the implementation. Enable it via the CLI options file, Gradle task, or Maven mojo to make object contracts explicit.
- A schema counts as an object when it declares
type: object(even with no properties) or when it declares properties without a type; other types, compositions without properties of their own, and inferred objects with empty properties maps are ignored. - Declaring
additionalPropertiesas a boolean or a schema satisfies the rule. - Model-level findings no longer print
property 'null'in the error message. - Wires the new option through
ValidatorParameters, the CLI, the Gradle plugin, and the Maven plugin.
Fixes #741.