Ajv $ref problem
See original GitHub issueI found that the ajv can’t resolve
$ref:#/components/schemas/someSchema
which is supposed to be supported by openapi 3. After I studied some of the source code, I found that foal didn’t adding the whole openapi.json schema when initializing the ajv instance, but only validate against a part of the schema.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
relative $ref not resolving · Issue #877 · ajv-validator/ajv - GitHub
6.5.4 When trying to resolved relative $ref, the algorythm returns there error: MissingRefError: can't resolve reference identifier_info_schema.json#...
Read more >Combining schemas - Ajv JSON schema validator
Combining schemas with $ref. You can structure your validation logic across multiple schema files and have schemas reference each other using $ref keyword....
Read more >How to use ajv with dependent json-schemas? - Stack Overflow
The error is because you're trying to use draft-04 of JSON Schema, which is no longer supported by ajv. If you want to...
Read more >ajv-validator/ajv - Gitter
If you expect it to fail because this ref is not present in schema, it will fail when you compile something that uses...
Read more >Untitled
#### Why Ajv does not replace references (\$ref) with the actual referenced schemas as some validators do? 1. The scope of Ajv is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Possible implementations of the solution of the issue 2
The description below is a summary of the in-depth study that took into account performance considerations, the difficulty/feasability, the amount of work required and the maintainability and testability of the new code.
It is mostly a help for me to implement the feature
Implementation 1: the framework replaces the values of the $ref keyword in the hooks
Idea: replace the
$ref
value with another link to the schema to get rid of the#
.Cons:
$ref
value will probably cause performance issues.#
, so this won’t work.Implementation 2: use one global AJV schema so that
#
works.Idea: build a global AJV schema which contains the OpenAPI component schemas.
Cons:
/api
is but the pages routes are not).Implementation 3 (the one chosen): use local AJV schemas
Idea: keep a local schema in each hook which is compiled the first time the hook is executed. Add the document component schemas to the hook schema.
I don’t think this implementation would lead to peformance issues but we’ll have to check.
Pros: the cons of implementation 2
The last difficulty that we have to face is to get the component schemas of the OpenAPI document which the controller belongs to.
For this, an
OpenApiService
can be added to register the OpenApi specifications(s) with their controller instances (then the hook will be able to know which API it belongs to).One way to do this (not chosen)
A boot method in the controller that creates the specs when launching the application and register them into the service.
Cons:
The other (right) way (chosen)
Refactor the core code so that the documents (known by
@ApiInfo
) and the app routes are built at the same moment. At this moment the document are registered in the service.Pros:
Cons:
Feature added in v2