Open moberegger opened 2 months ago
Hi. Thanks for the report. In general, and also related to https://github.com/ahx/openapi_first/issues/285, I would like to change from "dereference everything" to "dereference everything outside JSON Schema schemas" and let json_schemer handle $refs inside JSON Schemas (local and across files) correctly via it's .openapi
interface. Do you think that makes sense and could work?
I have not fully wrapped my head around that, so I think your option sounds good as well. A PR would be very much appreciated. If you don't get a fix to work, just a failing test would also be much appreciated.
When providing
openapi_first
a spec containing a schema likeit hits a
stack level too deep (SystemStackError)
error.I have a general idea of what is happening.
openapi_first
is successfully loading the schema, but in doing so it creates self referencing hashes as it de-$ref
erences everything. So it basically ends up doing something like this:This schema gets passed off to
json_schemer
to build a newJSONSchemer::Schema
which works until something is executed on it. For example:This happens because
openapi_first
de-references everything to keep the entire schema local. It doesn't know that'#/component/schemas/BoomRef'
is self referencing, so it de-references that and basically picks itself up from thefile_cache
and creates a self referencing hash. This causes an error when trying to any recursive work on it; in the example above,json_schemer
attempts adeep_stringify_keys
that runs forever until the stack error hits.There are a few ways to do self referencing schemas like this in Openapi. One way is like
but during dereferencing
openapi_first
sees that'#'
and tries to load it from a file, which results in an error.Another way is
which seems to work, but requires changes to the schema.
It would be nice if
openapi_first
could handle that first schema. I think what we could do is compareobject_id
s during the de-referencing process, and if we encounter a schema whoseobject_id
matches theobject_id
of the thing it is attempting to make a$ref
to, we simply swap that value out with a'#'
.Additionally, it would be nice if
openapi_first
could handle the second schema example. I think what we could do here is modifyDereferencer
slightly so that when it encounters a bare'#'
it skips trying to resolve it and instead just uses that as the$ref
value.Do you have any thoughts on this? I think I have a pretty good idea of how it works, so I could try submitting a PR to accommodate those if you find either solution acceptable.