Repository navigation
Conversation
Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
| * | ||
| * Remote (http(s)://) refs are out of scope and are left unresolved. | ||
| * | ||
| * @author eric.wittmann@gmail.com |
There was a problem hiding this comment.
Why are you giving me credit for this? 🤣
EricWittmann
left a comment
There was a problem hiding this comment.
Thanks for this. The approach is sound: a new FileReferenceResolver is plugged into Library.dereferenceDocument after the document is converted to 3.1 and before pre-processing. The change is small, and nothing happens unless the spec is loaded from a local file. The issues below should be fixed before merging.
Must fix
-
The generated
META-INF/openapi.jsonstill contains the external$ref. The expected output shows"$ref": "./multi-file-common.json#/components/schemas/Pet"copied unchanged into the generated project.multi-file-common.jsonisn't in that project, so anything that serves this file at runtime (e.g. MicroProfile OpenAPI / Swagger UI) will hit a broken reference. Either write out the dereferenced document, or state this limitation explicitly and add a test for it. -
openApiDocBaseDircan be left over from an earlier load. OnlysetOpenApiDocument(URL)sets it. If the sameOpenApi2JaxRsinstance is later given aStringorInputStream, the old directory is reused and refs resolve against the wrong place. Reset it tonullin those methods, or add an explicitsetBaseDir(Path)so String/InputStream callers such as quarkus-openapi-generator can also use this feature. As written, the main use case from #499 only works through the URL path. -
Load failures are silently ignored.
loadDocumentcatchesIOException | RuntimeExceptionand returnsnull. A missing file, a YAML typo or a non-object root (theObjectNodecast) all end up as an unresolved ref, and the user gets confusing generated code later instead of an error. At minimum log a warning with the path and the cause; failing fast is probably better.
Should fix
-
Tests only cover the simplest case. The Javadoc says it supports YAML, refs that chain across files and directories, files that contain only a fragment (no
openapiheader), and whole-file refs. The single test is one JSON file pointing at a sibling JSON file. Please add tests for:- a YAML external file
- a nested relative ref from a subdirectory (this is what the base-dir tagging exists for)
- a fragment-only file
- a ref to a file that doesn't exist
- two files that reference each other (the cache should prevent infinite recursion; confirm
dereferenceDocumenthandles it)
-
Base-dir tagging may not survive the import. Nested refs depend on node attributes surviving when the content is imported or copied into the main document. The Javadoc says they do, but nothing tests it. If the library copies nodes rather than re-parenting them, the tags are lost and nested refs quietly fall back to the root directory. This needs a test (see item 4).
-
Refs can reach any file on disk. A ref like
../../../../etc/passwdis read without restriction. That's low risk for local codegen, but it matters if this ever runs on user-supplied specs on a server. Consider limiting refs to stay underrootBaseDir, or document the behaviour.
Nits
- In
FileReferenceResolver,@author eric.wittmann@gmail.comlooks copied from another file; it should probably be you. - The
isRemoteregex is compiled on every call; make it astatic final Pattern. - The Javadoc on
dereferenceExternalRefshas an empty@param document, andresolveBaseDirhas a trailing blank Javadoc line. - The test fixtures mix versions (entry file is
3.0.2, common file is3.1.0). That's fine and arguably a good test, but a short comment saying it's deliberate would help.
Description
Adds support for resolving
$refsthat point at other local files (e.g../common-spec.yaml#/components/schemas/Pet), not just same-document refs.Previously every consumer (e.g. quarkus-openapi-generator) had to pre-flatten multi-file specs themselves before handing a single JSON document to this library.
Closes #499