wolverdude / GenSON

GenSON is a powerful, user-friendly JSON Schema generator built in Python.
MIT License
571 stars 64 forks source link

propertyName? to generate a nullable property in a schema #59

Closed etoiledemer closed 2 years ago

etoiledemer commented 2 years ago

Hi I want to convert a deeply nested JSON object using the command line; The deeply nested JSON object I want to convert contains many optional properties so I have to create a lot of files to simulate all the possible optional cases where some property can be null: So I do genson file1.json file2.json file3.json ....

However it's cumbersome.

I would appreciate if GenSON would support the possibility to add a ? to a property name to indicate that the property is nullable/not required. It would simplify a lot

example: https://pub.dev/packages/json_to_model supports appending a ? to a property name in the source JSON object to show it is nullable; As you see in the example ^^, the caseId property is written caseId? in the source JSON object so the schema generated takes into account the fact that the caseId can be null;

wolverdude commented 2 years ago

Hi @etoiledemer,

I cannot support the ? syntax because it is not consistent with the JSON-Schema standard upon which GenSON is based. However, I think GenSON already has features you can use to get around your problem.

Correct me if I'm wrong, but it sounds like you have a known schema you're starting with, and you want to feed that to GenSON as a starting point. You don't need to create every possible object in order to do this; just pass in a seed schema. Using your example of caseId, you might pass in a seed schema like this:

{
  "type": "object"
  "properties": {
    "caseId": {"type": "null"}
  }
}

If that isn't quite flexible enough for you, you can implement the ? syntax yourself by extending GenSON with a custom SchemaBuilder.

Also, if you still need to feed in a lot of files to the genson command, you can simplify your life by using a glob pattern, e.g.

genson *.json

I hope that helps. I'm closing this issue for now, but if you have more to say, feel free to reopen it. Thanks for reaching out!