Best way to extend JSON schema parsing #1781
Unanswered
WilliamJamieson
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have been looking into ways to automatically generate data models for a large and growing collection of schemas that I need to work with. After some experimentation and exploration it appears that the
datamodel-code-generator
appears to be a good project to build what I need off of. However, due to the peculiarities of the schemas I have to work with, the standard JSON schema parser used bydatamodel-code-generator
(or any similar project) is not sufficient to capture some of the features I need to extract from my schemas. Thus I believe that I will have to do some extensions to the JSON schema parser that is currently present.For background, the core of my issue is that the schemas in question employ an extension of JSON schema. This extension essentially adds a mechanism,
tag
, which is roughly equivalent to$ref
but it carries additional information. Namely, our application of JSON schema is validation of YAML data which includes sections which are tagged with YAML tags. Our validation of thetag
works via validating the data using the schema associated with the specific tag including any refinements usingallOf
combiners. However, in addition to the$ref
-like behavior, thetag
listed in the schema is also validated against the YAML tag present. While we currently have a system in place to validate data already written to these YAML files, we do not have a good system (its an entirely ad-hoc data model like scheme) to work with building data to write; hence, the desire to generate data models matching the schemas.After doing some prototyping with the current version of
datamodel-code-generator
I have found that I can achieve most of functionality I need. I can do this in two steps:JsonSchemaObject
, call itTaggedJsonSchemaObject
, to include atag
field and then adding amodel_post_init
method to that extension which looks up the correct$ref
for a giventag
and then fills in that information.JsonSchemaParser
to then employTaggedJsonSchemaObject
instead ofJsonSchemaObject
.This ends up solving most of the issues I have, but getting the
JsonSchemaParser
extension to work properly requires duplicating several methods exactly with the only change being replacingJsonSchemaObject
withTaggedSchemaObject
. These are:parse_combined_schema
parse_raw_obj
_parse_file
While this approach works, it does not feel like a good approach as it is quite prone to being broken by changes to
JsonSchemaParser
in the future. Thus my question(s):datamodel-code-generator
be interested in an enhancement to make it easier to replace theJsonSchemaObject
inJsonSchemaParser
?Edit:
My suggested enhancement is something akin to these changes.
Beta Was this translation helpful? Give feedback.
All reactions