Issue generating nested interface object #320
-
Hello team, This is my first issue in this repo. If nothing else, big thanks for this project. It is easy to understand, easy to use. I wanted to reach out highlighting an issue generating a nested interface object please. Please allow me to show a simple concrete example. I have a class Farm which contains one (it is an example) Animal (interface). The concrete Animal would be a Cow, Sheep, whatever.
I am using this code in order to generate the schema:
And I was hoping to get something like this: (Expected result)
or the similar with the sheep, basically, the concrete class of Animal However, I am getting this: (Actual result)
Could you please help on this issue to generate the schema for a nested class please? Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @patpatpat123, Thank you for the kind praise. 😄 On the matter of handling subtypes, there are a couple of ways to go about it. Main OptionsTarget Type OverrideIf you want to explicitly replace the configBuilder.forFields()
.withTargetTypeOverridesResolver(field -> field.getType().getErasedType() == Animal.class
? List.of(field.getContext().resolve(Cow.class)) : null); Subtype ResolverYou can configure a "Subtype Resolver", which identifies all the subtypes of a given interface/superclass. configBuilder.forTypesInGeneral()
.withSubtypeResolver((declaredType, generationContext) -> {
if (declaredType.getErasedType() == Animal.class) {
TypeContext typeContext = generationContext.getTypeContext();
return List.of(
typeContext.resolveSubtype(declaredType, Cow.class),
typeContext.resolveSubtype(declaredType, Sheep.class)
);
}
return null;
}); You can also use a library like "classgraph" to automatically find all eligible subtypes instead of explicitly mentioning them like this. Jackson ModuleYet another option would be to leverage existing annotations that indicate what the relevant subtypes are. In case you were using the Jackson libraries for serializing your data to JSON, you might already have configBuilder.with(new JacksonModule()); ComparisonThe aforementioned options are not the only ones available, but they are intended to be the most convenient ways to go about this. There are slight differences though:
SummaryYou have various ways to tackle this. It all depends on what information (e.g. in annotations) is already present in your code and how flexible it needs to be. |
Beta Was this translation helpful? Give feedback.
Hi @patpatpat123,
Thank you for the kind praise. 😄
On the matter of handling subtypes, there are a couple of ways to go about it.
Main Options
Target Type Override
If you want to explicitly replace the
Animal
interface with a single subtype and effectively hide the fact that in your code it is declared just asAnimal
, then you can use a "Target Type Override".Subtype Resolver
You can configure a "Subtype Resolver", which identifies all the subtypes of a given interface/superclass.
As it so happens, th…