Skip to content

Fix #407: validate required fields of inline models inside allOf and array items - #809

Open
Inf1mum wants to merge 3 commits into
OpenAPITools:masterfrom
Inf1mum:fix/407-required-fields-inside-allof
Open

Inf1mum wants to merge 3 commits into
OpenAPITools:masterfrom
Inf1mum:fix/407-required-fields-inside-allof

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Nov 7, 2026 •

Copy link
Copy Markdown

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 / oneOf compositions
  • items of 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 allOf with a missing required property produces the same error as a top-level model
  • an inline model inside allOf that satisfies its constraints produces no errors (no false positives)
  • an inline model inside array items with a missing required property is reported as well
  • partial oneOf branches with the required properties declared on the composing schema produce no errors (review example)
  • allOf: [{$ref: Base}, {required: [id]}] with id declared in the referenced Base produces no errors (review example)
  • negative controls: required names declared nowhere in the composition (oneOf and allOf/$ref cases) 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 $ref sibling 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 items validation.

Written for commit 6f63115. Summary will update on new commits.

View guided diff

…e allOf and array items

Required-property validation only ran on top-level component schemas, so
an object defined inline inside an allOf (or anyOf/oneOf) composition, or
as the items of an array schema, was never checked.

The check now recurses into the sub-schemas of compositions and array
items and reports missing required properties under the name of the
composing model. Sub-schemas that are pure $ref are skipped: referenced
component schemas are already validated on their own, which also avoids
duplicate errors when a parser resolves refs inline. A visited set keeps
the traversal finite for shared or cyclic schema structures.

Rule options semantics are unchanged.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

The required-property check inside compositions only looked at each
branch's own properties, so valid partial branches were reported as
missing required properties: a parent with properties {id, name} plus
oneOf [{required: [id]}, {required: [name]}], or allOf [$ref: Base,
{required: [id]}], produced 'property id should be present' although
the property is declared elsewhere in the composition.

Required names are now checked against every property name declared
across the 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 required
name present in any branch is known and no longer reported; names
declared nowhere are still reported. Array items keep their own
composition scope, since they describe different instances.

See the review discussion on PR OpenAPITools#809
(OpenAPITools#809 (comment)).

Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

In OpenAPI 3.1 a schema may declare keywords next to its $ref, e.g.
{$ref: '#/components/schemas/Base', required: [id]}, or the same with
properties or compositions. collectCompositionPropertyNames treated any
$ref as a short-circuit and returned only the referenced component's
names, discarding the referencing schema's own siblings, so a required
name declared only in those siblings was reported as missing.

The names collected from the resolved component schema are now merged
with the referencing schema's own properties and composition members
instead of replacing them, and a $ref member's sibling keywords are
validated in place: its own required list is checked against the
composition-wide known names while the referenced component schema
keeps being validated under its own name. The visited guard is
unchanged.

See the review discussion on PR OpenAPITools#809
(OpenAPITools#809 (comment)).

Signed-off-by: Inf1mum <vadimlazuk5@gmail.com>

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Required fields are not validated when creating a model inside an allOf

1 participant