Fix #407: validate required fields of inline models inside allOf and array items
Description
Fixes #407.
Required-property validation only ran on top-level component schemas. When a model was composed with allOf and one of the sub-schemas was defined inline (an anonymous type: object with its own required list), that inline model was never traversed, so missing required properties went unreported.
Reproduction
From the issue — this spec passes validation today, although the inline object requires something which it does not define:
components:
schemas:
Composition:
allOf:
- type: object
required:
- something
properties:
someotherthing:
type: array
items:
$ref: '#/components/schemas/Another'
- $ref: '#/components/schemas/Other'
Other:
type: string
example: "Other"
Another:
type: string
example: "Another"
With this change it reports:
*ERROR* in Model 'Composition', property 'something' -> This property should be present or removed from the list of required
Changes
validateModelRequiredProperties now recurses into the sub-schemas of a model:
allOf/anyOf/oneOfcompositionsitemsof array schemas
Errors on inline models are reported under the name of the composing component model. Semantics of the existing rule options (e.g. validateModelRequiredProperties) are unchanged.
Sub-schemas that are pure $ref are skipped: the referenced component schemas are already validated on their own, and skipping avoids duplicate errors when a parser resolves references inline. A visited set keeps the traversal finite for shared or cyclic schema structures.
Required names are checked against every property name declared across the whole composition tree rooted at the checked schema — the schema itself plus all its allOf/anyOf/oneOf members, transitively, following $ref members through the component schemas they point to. A valid composition may declare a required property in any of its branches instead of the composing schema itself (e.g. partial oneOf branches like {required: [id]} / {required: [name]} over a parent declaring {id, name}, or allOf: [{$ref: Base}, {required: [id]}] with id declared in Base), so a required name present in any branch is known and not reported. Required names declared nowhere in the composition are still reported, and array items keep their own composition scope, since they describe different instances.
Tests
Added cases to OpenApiSpecStyleValidatorTest:
- an inline model inside
allOfwith a missing required property produces the same error as a top-level model - an inline model inside
allOfthat satisfies its constraints produces no errors (no false positives) - an inline model inside array
itemswith a missing required property is reported as well - partial
oneOfbranches with the required properties declared on the composing schema produce no errors (review example) allOf: [{$ref: Base}, {required: [id]}]withiddeclared in the referencedBaseproduces no errors (review example)- negative controls: required names declared nowhere in the composition (
oneOfandallOf/$refcases) are still reported
All 25 tests of the class pass (lib and cli modules green, full ./gradlew spotlessApply test build on JDK 17).
Summary by cubic
Fixes missing required-property validation for inline models defined inside allOf/anyOf/oneOf compositions and array items. Previously only top-level component schemas were checked, so an anonymous type: object with a required list nested in a composition never got validated.
- Recurses into composition sub-schemas and array
items, reporting errors under the composing model's name, with a visited set to keep traversal finite for cyclic or shared schemas. - Required names are matched against properties declared anywhere in the composition tree, including
$ref'd schemas and OpenAPI 3.1$refsibling keywords, so valid partial branches aren't reported; names declared nowhere still are. - Adds tests for the error cases, no-false-positive composition cases, and array
itemsvalidation.