You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hej. I tried to update from v0.12.x to latest and spotted that library can't properly resolve multifile spec.
I managed to debug it and found why, but i don't quite get how to fix it.
So the dir/file layout that will reproduce the issue is the following (I'll only cover relevant parts, not all spec):
./lib/openapi.yaml <- main spec
./lib/resources/actions/endpoint.yaml <- endpoint description
./lib/shared/parameters.yaml <- parameters that endpoint refers to using relative $ref
explodedRefValue[0] will hold file path component of the $ref, in our case relative path component. And it is relative to the current file.
But resolution happens relative specPath which is always openapi.yaml. So it actually can't find what it is looking for. :(
Hmm, ok, yeah this whole system was overhauled a month ago and it looks like there are still some small glitches here and there. We did slam this code through thousands of multi-file tests, some that were beyond nuts, but there are multiple places where this logic is built out.
I will look into this ASAP, all the parts are there, something is just mis-aligned somewhere and this use case exposes it.
I did some investigation and found out that lowbase.SchemaProxy.ctx doesn't have key value pair of index.CurrentPathKey, "<basedir>/spec-ext.yaml" when it's acquired from model.Components.Schemas.Value("ExternalRef").GoLow(), and extraction_functions.go uses this value to decide where to look for the file. Strangely enough, if Schema Proxy is acquired via model.Parameters...GoLow(), it has correct context value.
X-Sender-FBS:
name: X-Sender-FBSin: headerdescription: FBS ID of the unit sending the request.required: trueschema:
type: stringnullable: true
Then, I wrote the following code.
package main
import (
"fmt""log/slog""os""time""github.com/pb33f/libopenapi""github.com/pb33f/libopenapi/datamodel"
)
funcreadExploded() []byte {
data, err:=os.ReadFile("resolve_components_updated/openapi.yaml")
iferr!=nil {
panic(err)
}
returndata
}
funcmain() {
check()
}
funccheck() {
start:=time.Now()
config:= datamodel.DocumentConfiguration{
IgnorePolymorphicCircularReferences: true,
IgnoreArrayCircularReferences: true,
Logger: slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelDebug,
})),
BasePath: "resolve_components_updated/",
}
doc, err:=libopenapi.NewDocumentWithConfiguration(readExploded(), &config)
iferr!=nil {
panic(err)
}
m, dErr:=doc.BuildV3Model()
ifdErr!=nil {
panic(dErr)
}
ifm!=nil {
fmt.Printf("model built\n")
}
end:=time.Now()
elapsed:=end.Sub(start)
fmt.Printf("done in %v\n", elapsed)
// let's pull out the description of the parameters, this can only be done if the model// has correctly resolved.wanted:="FBS ID of the unit sending the request."got:=m.Model.Paths.PathItems.GetOrZero("/actions").Parameters[0].Descriptionifwanted!=got {
panic("wanted: "+wanted+" got: "+got)
} else {
fmt.Println("everything is good")
}
}
What is the output?
{"time":"2023-12-18T09:02:32.04447-05:00","level":"DEBUG","msg":"[rolodex file loader]: extracting file from OS","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/resources/actions/endpoint.yaml"}
{"time":"2023-12-18T09:02:32.044642-05:00","level":"DEBUG","msg":"[rolodex file loader]: reading local file from OS","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/resources/actions/endpoint.yaml"}
{"time":"2023-12-18T09:02:32.044799-05:00","level":"DEBUG","msg":"[rolodex file loader]: extracting file from OS","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/shared/parameters.yaml"}
{"time":"2023-12-18T09:02:32.044804-05:00","level":"DEBUG","msg":"[rolodex file loader]: reading local file from OS","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/shared/parameters.yaml"}
{"time":"2023-12-18T09:02:32.044972-05:00","level":"DEBUG","msg":"[rolodex file loader]: successfully loaded and indexed file","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/shared/parameters.yaml"}
{"time":"2023-12-18T09:02:32.072306-05:00","level":"DEBUG","msg":"[rolodex file loader]: successfully loaded and indexed file","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/resources/actions/endpoint.yaml"}
{"time":"2023-12-18T09:02:32.072393-05:00","level":"DEBUG","msg":"[rolodex] starting root index build"}
{"time":"2023-12-18T09:02:32.072396-05:00","level":"DEBUG","msg":"[rolodex] root index build completed"}
model built
donein 29.210542ms
everything is good
This all resolves exactly as it should, you can see the rolodex under the covers is locating everything without issue and the
resolved description matches exactly.
I did some investigation and found out that lowbase.SchemaProxy.ctx doesn't have key value pair of index.CurrentPathKey, "<basedir>/spec-ext.yaml" when it's acquired from model.Components.Schemas.Value("ExternalRef").GoLow(), and extraction_functions.go uses this value to decide where to look for the file. Strangely enough, if Schema Proxy is acquired via model.Parameters...GoLow(), it has correct context value.
This is actually a bug, you discovered a small edge case where schema builds were not carrying down context. I was able to replicate this issue, and located the problem.
Activity
daveshanley commentedon Dec 14, 2023
Hmm, ok, yeah this whole system was overhauled a month ago and it looks like there are still some small glitches here and there. We did slam this code through thousands of multi-file tests, some that were beyond nuts, but there are multiple places where this logic is built out.
I will look into this ASAP, all the parts are there, something is just mis-aligned somewhere and this use case exposes it.
yozel commentedon Dec 18, 2023
I'm not sure if this is the same issue, but I have encountered something similar.
spec.yaml:
spec-ext.yaml:
With these two files, the following code returns an error:
While this creates the schema without any errors:
I did some investigation and found out that
lowbase.SchemaProxy.ctxdoesn't have key value pair ofindex.CurrentPathKey, "<basedir>/spec-ext.yaml"when it's acquired frommodel.Components.Schemas.Value("ExternalRef").GoLow(), and extraction_functions.go uses this value to decide where to look for the file. Strangely enough, if Schema Proxy is acquired viamodel.Parameters...GoLow(), it has correct context value.yozel commentedon Dec 18, 2023
If it helps, here is the go tests I wrote yozel@4b9eb63
daveshanley commentedon Dec 18, 2023
Hi @bitomaxsp,
So I loaded this exact test into my testrig to locate the issue and I cannot re-create the problem.
openapi.yamlresources/actions/endpoint.yamlshared/parameters.yamlThen, I wrote the following code.
What is the output?
{"time":"2023-12-18T09:02:32.04447-05:00","level":"DEBUG","msg":"[rolodex file loader]: extracting file from OS","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/resources/actions/endpoint.yaml"} {"time":"2023-12-18T09:02:32.044642-05:00","level":"DEBUG","msg":"[rolodex file loader]: reading local file from OS","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/resources/actions/endpoint.yaml"} {"time":"2023-12-18T09:02:32.044799-05:00","level":"DEBUG","msg":"[rolodex file loader]: extracting file from OS","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/shared/parameters.yaml"} {"time":"2023-12-18T09:02:32.044804-05:00","level":"DEBUG","msg":"[rolodex file loader]: reading local file from OS","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/shared/parameters.yaml"} {"time":"2023-12-18T09:02:32.044972-05:00","level":"DEBUG","msg":"[rolodex file loader]: successfully loaded and indexed file","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/shared/parameters.yaml"} {"time":"2023-12-18T09:02:32.072306-05:00","level":"DEBUG","msg":"[rolodex file loader]: successfully loaded and indexed file","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components/resources/actions/endpoint.yaml"} {"time":"2023-12-18T09:02:32.072393-05:00","level":"DEBUG","msg":"[rolodex] starting root index build"} {"time":"2023-12-18T09:02:32.072396-05:00","level":"DEBUG","msg":"[rolodex] root index build completed"} model built done in 29.210542ms everything is goodThis all resolves exactly as it should, you can see the rolodex under the covers is locating everything without issue and the
resolved description matches exactly.
daveshanley commentedon Dec 18, 2023
This is actually a bug, you discovered a small edge case where schema builds were not carrying down context. I was able to replicate this issue, and located the problem.
Fixed glitch reported by #219
Fixed glitch reported by #219
daveshanley commentedon Dec 18, 2023
@yozel If you try v0.14.1 you should see the result you're expecting now. This is my result when running against your specs
{"time":"2023-12-18T09:55:02.518305-05:00","level":"DEBUG","msg":"[rolodex file loader]: extracting file from OS","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components_updated/spec-ext.yaml"} {"time":"2023-12-18T09:55:02.518468-05:00","level":"DEBUG","msg":"[rolodex file loader]: reading local file from OS","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components_updated/spec-ext.yaml"} {"time":"2023-12-18T09:55:02.518799-05:00","level":"DEBUG","msg":"[rolodex file loader]: successfully loaded and indexed file","file":"/Users/daveshanley/pb33f/libopenapi-testrig/resolve_components_updated/spec-ext.yaml"} {"time":"2023-12-18T09:55:02.51886-05:00","level":"DEBUG","msg":"[rolodex] starting root index build"} {"time":"2023-12-18T09:55:02.518866-05:00","level":"DEBUG","msg":"[rolodex] root index build completed"} model built done in 1.863ms wanted: string got: stringyozel commentedon Dec 18, 2023
@daveshanley the issue is resolved. Thanks for the quick fix! I discovered it while I was trying to migrate https://github.com/deepmap/oapi-codegen into libopenapi, and now I the pull request there is complete without any failed tests
Fixed glitch reported by pb33f#219