Include FQCN (fully qualified class name) in JSON schema
See original GitHub issueHi,
I work with a team of Java developers, and we use JSON Schemas to normalize data that is sent over Kafka.
My application is not Java. They ask me to send the FQCN of the serialized object when sending JSON payloads in Kafka.
As I am generating code from the JSON schemas, I think I could store that FQCN somewhere in the generated code, so it is available when I normalize and serialize the payloads. That is, if I can find that FQCN when generating the code. So I would like it to be present in the schema directly.
Is there a way to do that? I was thinking of adding a custom field in the schema, for instance $fqcn
(at the top level just like "$schema" : "http://json-schema.org/draft-07/schema#"
.)
If possible, would it be also possible to include it for all embedded objects (objects that are in a property of a parent object and described in the properties
object of the schema).
Thank you.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
OK !
In our classes we add the JSON annotation to the properties so those discriminant properties so I guess that is not a limitation for us, they are present in the schema, for instance : https://github.com/lud/victools-fqcn-demo/blob/1fed3ffb9239782cf192e0a20d818134d1c6f6f0/src/main/resources/schemas/CreatePage-schema.json#L6
As you may have guessed, we are not really using the generated schemas to be used as actual JSON schemas. We just need a language agnostic definition of some data structures, and victools is really helpful for it. So I would not bother change the library just for that specific use case ; but hey, If you think it can be useful for other purposes, why not.
That was kind of hard because of my limited knowledge of java but I got it to work with your suggestions : https://github.com/lud/victools-fqcn-demo/blob/1fed3ffb9239782cf192e0a20d818134d1c6f6f0/src/main/java/com/myapp/contracts/serialization/FqcnModule.java#L35
Thanks a lot!
I can confirm that neither
CustomDefinitionProvider
/CustomPropertyDefinitionProvider
norTypeAttributeOverride
/InstanceAttributeOverride
currently receives the node directly containing the"anyOf"
on a respective property.That is currently due to your overriding of the applicable subtypes on the respective fields instead of declaring it on the
IPage
interface type in general. Please also note thatinclude = JsonTypeInfo.As.EXTERNAL_PROPERTY
is currently not supported. The external property will not be added/defined correctly in the schema!As to accessing the
"anyOf"
: I guess I could change it so that at least theInstanceAttributeOverride
is being applied there again (in addition to being applied on each subtype version of the property). This combination is likely not used so widely yet to prohibit this. May need to consider indicating in some obvious manner, when a given property schema is (a) the declared type without any subtypes being applied, (b) an overridden subtype, © the declared type but where subtypes exist as well.For now, you could work around this limitation by utilizing a
TypeAttributeOverride
and then iterating over its properties – checking whether any of them contain an"anyOf"
directly. But I can appreciate it if that’s not a feasible option for you.Either way, would be worth creating a separate issue for this, as the original question here for the fully qualified class name being included (in most circumstances) has been answered.