Skip to content

Support contentMediaType for OpenAPI 3.1 schemas (parse + ContentMediaTypeIn30 rule) - #204

Open
takayamaki wants to merge 4 commits into
ota42y:masterfrom
takayamaki:pr14-contentmediatype
Open

Support contentMediaType for OpenAPI 3.1 schemas (parse + ContentMediaTypeIn30 rule)#204
takayamaki wants to merge 4 commits into
ota42y:masterfrom
takayamaki:pr14-contentmediatype

Conversation

@takayamaki

Copy link
Copy Markdown
Contributor

Continuing the OpenAPI 3.1 work from #152.

OpenAPI 3.1 adopts JSON Schema 2020-12, which brings the contentMediaType annotation.
It records the media type of a string that carries an embedded payload.
3.0 has no keyword for this.

This PR adds parse support and version-mismatch detection.

Parse layer

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

schema.contentMediaType # => "image/png"

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

contentMediaType is an annotation: it describes the payload rather than constraining it,
so there is no runtime validation to add here.

SpecValidator rule

ContentMediaTypeIn30 reports a violation for each schema in a 3.0 document that uses contentMediaType:

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

Detection inspects raw_schema key presence,
so it fires on any declared media type value.

`contentMediaType` on a 3.0 document warns and raises (JSON Schema
2020-12 annotation 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 27, 2026 23:57
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.

1 participant