Skip to content

openapi31: support 3.1+ reference metadata overrides#1215

Open
AlexanderWangY wants to merge 1 commit into
getkin:masterfrom
AlexanderWangY:openapi31-reference-metadata
Open

openapi31: support 3.1+ reference metadata overrides#1215
AlexanderWangY wants to merge 1 commit into
getkin:masterfrom
AlexanderWangY:openapi31-reference-metadata

Conversation

@AlexanderWangY

@AlexanderWangY AlexanderWangY commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Adds OpenAPI 3.1 Reference Object summary and description override support. This is described in the reference object spec.

Changed ref.go and refs.tmpl to support new summary and description fields excluding Schema type.

These overrides do NOT apply in OpenAPI 3.0, and therefore are ignored. Furthermore, only the following Reference Object types support overrides:

  • Example (summary and description)
  • Header (description only)
  • Parameter (description only)
  • RequestBody (description only)
  • Response (description only)
  • Link (description only)
  • SecurityScheme (description only)

These do not apply to CallbackRef or SchemaRef, as these fields are not allowed.

Some minor details to pay attention to:

  • If you don't capture IsOpenAPI31OrLater() at the start, it may be evaluated "after" resolution context changes.
  • Metadata resolution/overriding is done in both mutually exclusive paths
    • Normal reference resolution
    • shouldVisitRef is false resulting in a backtracking callback

Note: Updated the Issue #513 regression test because it previously treated description beside $ref as invalid in every version. summary and description are now recognized Reference Object fields and they override supported metadata in OAS 3.1 and are STILL ERRORS in OAS 3.0.

Testing

Added regression testing in loader_test.go.

You can try it out!

go test ./openapi3 -run '^TestReferenceObjectMetadataIsVersionAware$' -count=1

Unrelated

CI was not having a fun time with the formatting after template generation. Instead, I changed refsgenerator.go to feed the generated content into a formatter and then write to a file to preserve formatting. This is a purely cosmetic problem.

@AlexanderWangY

AlexanderWangY commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

This git differ is killing me. Ok so I just forgot to run doc generation.

@AlexanderWangY AlexanderWangY marked this pull request as draft July 9, 2026 01:16
@AlexanderWangY AlexanderWangY force-pushed the openapi31-reference-metadata branch from 04c4e37 to 79fca24 Compare July 9, 2026 01:25
@AlexanderWangY AlexanderWangY marked this pull request as ready for review July 9, 2026 01:32
@AlexanderWangY AlexanderWangY marked this pull request as draft July 9, 2026 01:33
@AlexanderWangY AlexanderWangY force-pushed the openapi31-reference-metadata branch 2 times, most recently from 72d27bb to 614fc99 Compare July 9, 2026 01:40
@AlexanderWangY AlexanderWangY marked this pull request as ready for review July 9, 2026 01:44
@AlexanderWangY

Copy link
Copy Markdown
Contributor Author

Rate limited in CI 🤣

Comment thread openapi3/refs.tmpl
Comment thread openapi3/ref.go
Comment thread openapi3/issue513_test.go Outdated
Add summary and description support for OpenAPI 3.1 Reference Objects, reject those extra fields for OpenAPI 3.0, and regenerate the API documentation.
@AlexanderWangY AlexanderWangY force-pushed the openapi31-reference-metadata branch from 97348dc to e562b6c Compare July 9, 2026 16:13
@AlexanderWangY AlexanderWangY requested a review from fenollp July 9, 2026 17:18

@fenollp fenollp 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. Some notes. Please rebase.

Comment thread openapi3/loader_test.go
wantDescription: "reference description",
},
} {
t.Run(test.name, func(t *testing.T) {

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.

Please add a t.Parallel()

Comment thread openapi3/loader.go
errMUSTSecurityScheme = errors.New("invalid securityScheme: value MUST be an object")
)

// applyHeaderRefMetadata applies OpenAPI 3.1 overrides without mutating the referenced Header.

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.

Unless I am mistaken, all these could mutate value directly. Also drop the comment line please.

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