Skip to content

Support const for OpenAPI 3.1 schemas (parse + runtime + ConstIn30 rule) - #200

Merged
geemus merged 4 commits into
ota42y:masterfrom
takayamaki:pr12-const
Aug 14, 2026
Merged

Support const for OpenAPI 3.1 schemas (parse + runtime + ConstIn30 rule)#200
geemus merged 4 commits into
ota42y:masterfrom
takayamaki:pr12-const

Conversation

@takayamaki

Copy link
Copy Markdown
Contributor

Continuing the OpenAPI 3.1 work from #152.

OpenAPI 3.1 adopts the JSON Schema 2020-12 const keyword, which pins a value to exactly one constant.
3.0 does not recognize it (the closest 3.0 idiom is a single-element enum).

This PR adds parse support, runtime validation, and version-mismatch detection.

Parse layer

const is parsed as a plain value and exposed as an accessor on Schema:

schema.const # => "fixed-value"

Following the permissive-parse strategy agreed in #152,
the parse layer accepts const regardless of the declared OpenAPI version.

Runtime validation

const is checked before type dispatch in validate_schema,
so it applies uniformly across primitives, arrays, and objects:

# schema: { const: "USD" }
request_operation.validate_request_body('application/json', { 'currency' => 'JPY' })
# => OpenAPIParser::ValidateError: "JPY" const "USD" invalid

Detection inspects raw_schema key presence,
so an intentional const: null still constrains the value to null.

SpecValidator rule

ConstIn30 reports a violation for each schema in a 3.0 document that uses const:

OpenAPIParser.load(
  'spec.yaml',
  strict_specification_version: :warn,
)
# [ConstIn30] #/components/schemas/Currency — `const` is a 3.1 addition (from JSON Schema 2020-12); 3.0 has no equivalent

…R12)

- Schema gains a `const` accessor via openapi_attr_values
- Rules::ConstIn30 flags the keyword on 3.0 documents
- validate_schema short-circuits to a ValidateError when const is
  present and value != schema.const; uses raw_schema.key? so an
  intentional const: null is honored
`const` on a 3.0 document warns and raises (JSON Schema 2020-12 keyword
with no 3.0 equivalent); the same keyword on a 3.1 document stays clean.
@takayamaki
takayamaki marked this pull request as ready for review August 11, 2026 07:11

@geemus geemus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

lgtm, thanks!

@geemus
geemus merged commit 7e3c482 into ota42y:master Aug 14, 2026
14 checks passed
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.

2 participants