feat: implement interface for alternative JSONSchema() function #130
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi there,
I have a particular use case of jsonschema where we have the following situation:
We have a
Foo
"enum" that has a limited set of known values, and implements various marshallers so that a string representation of the "enum" is used for all inbound and outbound serialization.Unfortunately we've noticed that when serializing a map with a key of
Foo
jsonschema only considers the underlying integer value when reflecting the schema and produces the following schema:Given that the JSON marshaller will (un)marshal map keys using the TextMarshaller interface, this is an issue for us. Perhaps their is also an argument that jsonschema should do the same, but my first approach to this was to try and provide an alternative schema using
JSONSchema
andJSONSchemaExtend
. However, I have noticed thatJSONSchema
in particular is only usable for leaf nodes, you cannot return the result of a newjsonschema.Reflect
as this may not have the same configuration as the parent and it will nest a new schema in the parent.JSONSchemaExtend
is possible to use for this, however it requires significant modification to the resulting schema in a way that I'm not personally comfortable with.With this merge request I've added an alternative definition of the
JSONSchema
interface that provides areflect
callback which can be used to continue reflection into children.The implementation of this for the foo map would be as follows:
My reasoning about using the same function name
JSONSchema
overJSONSchemaIntercept
or something similar, is because it keeps backwards compatibility while preventing implementing both interfaces on a single type, which would be a strange thing to do imo.I haven't written tests for this yet, I wanted to hear your opinions on it first.