Skip to content

fix(overlay): correctly resolve references after Overlay application - #1825

Merged
jamietanna merged 6 commits into
oapi-codegen:mainfrom
jgraeger:feat/external-references-with-overlay
Nov 28, 2024
Merged

jamietanna merged 6 commits into
oapi-codegen:mainfrom
jgraeger:feat/external-references-with-overlay

Conversation

@jgraeger

@jgraeger jgraeger commented Nov 7, 2024 •

Copy link
Copy Markdown
Contributor

When working with overlays, oapi-codegen doesn't support external references, even though they are supported for the same file without overlays.

This PR adds a new setting resolve-refs to the OverlayOutput configuration. If enabled, configures the oapi loader such as the external references are resolved as for a normal file (against the filepath of the spec).

@jgraeger
jgraeger requested a review from a team as a code owner November 7, 2024 16:23
@jamietanna

Copy link
Copy Markdown
Member

Interesting - would you mind adding an example of this to internal/test?

I've not hit this before, but would be good to see what we're missing

Comment thread configuration-schema.json Resolved
@jgraeger

jgraeger commented Nov 7, 2024 •

Copy link
Copy Markdown
Contributor Author

@jamietanna Thank you for the swift review! I agree and removed config flag and enabled it by default. I also added an example to internal/test for overlay.

Apparently the issue only occurs if the spec is in a diffent directory then the one oapi-codegen is invoked from. Therefore I put the spec.yaml into the subdirectory spec/. Running go generate after setting ResolveRefs to false reproduces the error.

@jgraeger

Copy link
Copy Markdown
Contributor Author

@jamietanna Is the example sufficient or do you need more information? :)

Jamie Tanna added 2 commits November 28, 2024 09:19
@jamietanna

Copy link
Copy Markdown
Member

Thanks very much for this @jgraeger - this was a good catch, and I'm classing this as a bug, not a feature 😅

I've pushed a couple of tweaks - will wait for CI then merge

Main tweaks:

  • no longer making it opt-in, and instead default behaviour
  • moving the test to a different directory - you weren't expected to know this, and this is more my personal preference, what you'd done was absolutely great - thank you!

@jamietanna
jamietanna merged commit cc97261 into oapi-codegen:main Nov 28, 2024
@jamietanna jamietanna changed the title feat: Allow resolving references after overlay application fix(overlay): correctly resolve references after Overlay application Nov 28, 2024
@jamietanna jamietanna added the bug Something isn't working label Nov 28, 2024
@pixelsoccupied

Copy link
Copy Markdown

Tried this fix on my side and it works well! Is there any ETA for this release @jamietanna? Thanks!

mromaszewicz added a commit to mromaszewicz/oapi-codegen that referenced this pull request Oct 31, 2026
…verlay

Move allof_merge_extensions and external_json_ref back to
references/overlays, unchanged from before this branch. They test allOf
merging (oapi-codegen#2335) and external references (oapi-codegen#1825) and only happen to use
an overlay, so they stay in their feature's category; internal/test/overlay
holds the tests of overlay application itself.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
mromaszewicz added a commit that referenced this pull request Oct 31, 2026
…ath (#2643)

* feat(overlay): apply overlays with an in-tree applier on theory/jsonpath

Overlay support came from github.com/speakeasy-api/openapi/overlay, whose
JSONPath chain (speakeasy-api/jsonpath, vmware-labs/yaml-jsonpath,
dprotaso/go-yit) breaks under `go get -u` since go-yit moved to
go.yaml.in/yaml/v4. Replace it with a small Overlay 1.0.0 applier in
internal/overlay, built on github.com/theory/jsonpath (MIT, no runtime
dependencies, strict RFC 9535), working on the spec as decoded JSON values
with json.Number so numeric literals keep their text.

The applier mirrors what the previous library did for 1.0.0 overlays:
the same validation, actions applied in order, recursive merge of
mappings, sequences appended to, other values replaced, removal from the
parent, and in strict mode an error listing every action whose target
matched nothing. Unlike before, a non-sequence update to a sequence
target is appended to it, as the Overlay specification says, instead of
replacing the sequence, and removing $ is an error instead of a no-op.

Targets are now RFC 9535 JSONPath. An invalid target's error suggests
the RFC 9535 form of a target written in the previous dialect, and names
the new compatibility.overlays-use-yaml-jsonpath option, which reads
targets in the previous dialect by translating them with
internal/overlay/legacyjsonpath. The examples/overlay targets are
rewritten in RFC 9535; the generated code is unchanged.

speakeasy-api/openapi and the modules it pulled in are no longer
dependencies of any module; github.com/oasdiff/yaml, already used by
kin-openapi, becomes a direct dependency to read overlay YAML.

Closes: #2502

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* test(overlay): consolidate overlay tests under internal/test/overlay

Overlay tests get a category of their own: the allof_merge_extensions and
external_json_ref leaves move there from references/overlays, with their
import paths and schema references updated.

Two new leaves, rfc9535 and legacy_jsonpath, apply overlays that express
the same actions, one in each JSONPath dialect, to one shared spec.yaml:
structural and filtered updates, renaming a type, removing a property,
removing x-internal schemas, properties, paths and operations, removing
by tag and by operationId, merging, appending to sequences and replacing
a scalar. The overlays also add oapi-codegen's extensions, several of
them through filtered targets: x-go-name on schemas and properties,
x-go-type with x-go-type-import, x-go-type-skip-optional-pointer,
x-omitempty, x-go-json-ignore and x-order.

overlay_test.go checks that both leaves embed the same document, the
expected effects on it, and that the generated code honours the added
extensions. Operations tagged internal carry no other tags, so the two
dialects' readings of a tag comparison agree; the difference is covered
by a unit test in internal/overlay/legacyjsonpath.

Generating the legacy_jsonpath leaf with the previous, speakeasy-based
oapi-codegen gives the same output, apart from the action that appends a
mapping to the top-level tags, which that version applied by replacing
the sequence.

The category list in .greptile/rules.md gains overlay/.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* docs(overlay): document RFC 9535 targets and the compatibility option

Rewrite the README's overlay example in RFC 9535 JSONPath and note the
main differences from the previous dialect, and that
compatibility.overlays-use-yaml-jsonpath keeps it.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* fix(overlay): accept overlay 1.1.x documents

The previous implementation accepted "overlay: 1.1.0", which it read as a
request for RFC 9535 targets. That is now what every overlay gets, so
rejecting the version would only break overlays that already moved to
it. Copy actions, the other 1.1.0 addition, are reported as unsupported.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* test(overlay): keep only overlay-specific tests under internal/test/overlay

Move allof_merge_extensions and external_json_ref back to
references/overlays, unchanged from before this branch. They test allOf
merging (#2335) and external references (#1825) and only happen to use
an overlay, so they stay in their feature's category; internal/test/overlay
holds the tests of overlay application itself.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* docs(test): describe the overlay test category in .greptile/rules.md

references/ keeps its overlays, and overlay/ is described as the tests
of overlay application itself.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants