jhthorsen / json-validator

:cop: Validate data against a JSON schema
https://metacpan.org/release/JSON-Validator
56 stars 58 forks source link

Add support for changing definitions path when working with $ref #177

Closed HEM42 closed 4 years ago

HEM42 commented 4 years ago

Summary

Allow for definitions path to be changed, today the default behavior for adding refs into /definitions/some_def will render a OpenAPI v3 spec wrongly.

a $ref like #/components/schemas/myschema will be rendered as /definitions/components_schemas_myschema, which is not valid according the OpenAPI v3 spec

the generate_definitions_path function makes it possible to override this behavior, keeping JSON::Validator neutral to the proposed change.

These changes is based on the generate_definitions_path branch, but with a fix for the test

Motivation

OpenAPIv3 has chosen to rearrange its definitions, into a deeper path as an example /components/schemas/myschema

This change makes it possible for modules like Mojolicious::Plugin::OpenAPI to support the v3, with minimal effort

Earlier attempts to make this right eq. #174 was not general enough

Hope that this time around the change is general enough to be accepted

References

jhthorsen commented 4 years ago

I'm not too fond of merge commits in PR's, so I pushed the changes directly in 98dbf69ba67b156f6a0371a585cdd586a97d1d79

Thanks for fixing the unit test 👍

I'm not sure how good this code is though, with _node() being called a "gazillion" times, but let's try it out for now.