question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Include FQCN (fully qualified class name) in JSON schema

See original GitHub issue

Hi,

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:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ludcommented, Mar 18, 2022

I can confirm that neither CustomDefinitionProvider/CustomPropertyDefinitionProvider nor TypeAttributeOverride/InstanceAttributeOverride currently receives the node directly containing the “anyOf” on a respective property.

OK !

Please also note that include = JsonTypeInfo.As.EXTERNAL_PROPERTY is currently not supported. The external property will not be added/defined correctly in the schema!

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 to accessing the “anyOf”: I guess I could change it so that at least the InstanceAttributeOverride 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.

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.

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.

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!

0reactions
CarstenWicknercommented, Mar 16, 2022

I can confirm that neither CustomDefinitionProvider/CustomPropertyDefinitionProvider nor TypeAttributeOverride/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 that include = 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 the InstanceAttributeOverride 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there any method to define the generated class name and ...
At firstly, I put a UserInfo.json in folder pojo/user/. ... fully qualified class name. ... When used in a schema, javaType defines the...
Read more >
Java Jackson serializer including FQCN - json - Stack Overflow
public class FQCNTest { @Test public void doTest() throws JsonProcessingException { final ObjectMapper om = getObjectMapper(); final Object ...
Read more >
Error in toString() when generating classes that share ...
I am attempting to generate Java classes from third party JSON schema, ... some reason the class `System.class` is not given its fully...
Read more >
Structuring a complex schema — Understanding JSON ...
This chapter will present the tools available for reusing and structuring schemas as well as some practical examples that use those tools. Schema...
Read more >
JSON Template Layout - Log4j –
Adding log4j-layout-template-json artifact to your list of dependencies is enough to ... Resolve the logger's fully qualified class name:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found