Skip to content

fix(codegen): read singular example under 3.1 - #2523

Open
bendrucker wants to merge 1 commit into
oapi-codegen:mainfrom
bendrucker:example-fallback-31
Open

fix(codegen): read singular example under 3.1#2523
bendrucker wants to merge 1 commit into
oapi-codegen:mainfrom
bendrucker:example-fallback-31

Conversation

@bendrucker

Copy link
Copy Markdown
Contributor

Under OpenAPI 3.1, every example: in a spec is dropped from the generated doc comments.

describeWithExamples branches on the document version. The 3.1 arm reads only schema.Examples, the plural JSON Schema array, and returns the description unchanged when that array is empty, never falling back to the singular value. OpenAPI 3.1 keeps example as a valid annotation, and specs written against 3.1 still use it heavily, so the singular keyword generates nothing at all.

Regenerating a real 3.1 spec showed the size of it: 380 // Example: comments on the 3.0 path, 0 on the 3.1 path.

The two paths are mirror images. Neither reads both keywords:

singular example plural examples
3.0 rendered ignored
3.1 ignored rendered

kin-openapi's schema unmarshalling has no version branch, so example: lands in schema.Example under 3.1 exactly as it does under 3.0. The value is parsed and present, just never read. That makes this a fallback in the codegen rather than anything to do with loading.

Change

Prefer schema.Examples when it's set and fall back to schema.Example, so 3.1 output matches 3.0 for the same input.

This only adds comments where the current code emits none. No types, names, or signatures move. Consumers regenerating against it see doc comments appear and nothing else change.

Scope

The symmetric change would be reading plural examples on the 3.0 path, which I left out. examples isn't a valid schema keyword in 3.0, so honoring it would invent comments from input that shouldn't be there, and it would churn checked-in generated files for every existing 3.0 user. The 3.1 direction has neither cost. It's a small change in the same function if you'd rather have the symmetry.

Tests

internal/test/openapi31/spec.yaml gains a Pet.nickname property using the singular keyword. TestPetExampleComments parses the generated source and asserts the comment, so the regression fails the suite if it comes back. TestDescribeWithExamples covers the version branches directly, including 3.1 preferring the plural array when a schema carries both keywords.

describeWithExamples read only schema.Examples when the document was 3.1,
returning early when that array was empty. OpenAPI 3.1 keeps the singular
example keyword as a valid annotation and specs written against 3.1 use it
heavily, so every such example was dropped from the generated doc comments.
kin-openapi parses example into schema.Example regardless of document
version, so the value was present and simply unread.

Prefer the plural array when set and fall back to the singular value, which
makes 3.1 output match 3.0 for the same input.
@greptile-apps

greptile-apps Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR restores singular example annotations in generated OpenAPI 3.1 documentation while retaining plural examples precedence.

  • Updates describeWithExamples to fall back from Schema.Examples to Schema.Example for OpenAPI 3.1.
  • Adds focused unit and OpenAPI 3.1 fixture coverage for fallback and precedence behavior.
  • Regenerates the fixture with only the expected field and documentation additions.

Confidence Score: 5/5

The PR appears safe to merge with no actionable defects identified.

The fallback preserves existing OpenAPI 3.0 behavior, keeps plural examples preferred under 3.1, and changes only generated documentation plus the fixture field introduced to test it.

Important Files Changed

Filename Overview
pkg/codegen/schema.go Implements the OpenAPI 3.1 singular-example fallback without changing generated API shapes.
pkg/codegen/schema_test.go Covers nil input, both version branches, precedence, absent examples, and structured rendering.
internal/test/openapi31/spec.yaml Adds a singular-example property to the existing OpenAPI 3.1 fixture.
internal/test/openapi31/openapi31_test.go Verifies the fixture retains its description and emits the singular example comment.
internal/test/openapi31/openapi31.gen.go Contains only the expected generated field and documentation output.

Reviews (1): Last reviewed commit: "fix(codegen): read singular example unde..." | Re-trigger Greptile

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