openapi icon indicating copy to clipboard operation
openapi copied to clipboard

spec validate accepts a number or boolean top-level `$ref` in OpenAPI 3.0 documents (since v1.26.4)

Open sronix opened this issue 2 weeks ago • 0 comments

Since v1.26.4, openapi spec validate reports a 3.0 document as valid when a schema's top-level $ref is a number or a boolean. v1.26.3 rejected it. I think this comes from #303 (the fix for #300): top-level $ref schemas in 3.0 now skip dialect validation, and that was the step that caught the wrong type.

Reproduction

r1-comp-ref-int-30.yaml:

openapi: 3.0.3
info:
  title: r1-comp-ref-int-30
  version: "1"
paths: {}
components:
  schemas:
    Base:
      type: number
    S:
      $ref: 1
openapi spec validate r1-comp-ref-int-30.yaml

Actual on v1.26.4 (exit 0)

Validating OpenAPI document: r1-comp-ref-int-30.yaml
✅ OpenAPI document is valid - 0 errors
Validation completed in 1ms

On v1.26.3 (exit 1)

Validating OpenAPI document: r1-comp-ref-int-30.yaml
❌ OpenAPI document is invalid - 1 errors:

1. [11:13] error validation-type-mismatch schema.$ref expected `string`, got `number`
Validation completed in 1ms
Error: openAPI document validation failed

$ref: true, $ref: false and $ref: 1.5 behave the same, and so does the same value in a parameter, header, request body or response schema. Still rejected on v1.26.4: the same document as openapi: 3.1.0, a nested $ref: 1 (under properties, items or allOf), and a null, list or map $ref. The error now shows up later instead: spec inline on a document that uses such a schema fails with open 1: no such file or directory.

Expected

Invalid, as on v1.26.3. #303 says "Top-level $ref schemas skip dialect validation (the reference string is still validated)", and the OpenAPI 3.0.3 Reference Object defines $ref as "string | REQUIRED. The reference string."

From reading the source at v1.26.4 (a guess, not tested): the top-level $ref is decoded as a string, so 1 becomes "1", which passes the reference-string check, and the new early return in jsonschema/oas3/validation.go skips the only step that saw the raw type. #303's tests cover a non-string nested $ref but not a top-level one.

Versions

openapi 1.26.4 (build 8989d069dfc8c30c4938930753b4c2e03c992b10, darwin arm64 release binary): valid, exit 0. openapi 1.26.3 (build 2aeb6618014d2a22c249ddffe0f8030503da1e8b) and 1.26.2 (build 08bfc549631f1bf9924e3426644424fcd6e13e33): rejected, exit 1.

Our AI agents found this while re-checking #300 on v1.26.4, using synthetic documents.

sronix avatar Nov 16 '26 19:11 sronix