Schema Generation fails with Duplicate Key exception when using JacksonModule and Option.DEFINITIONS_FOR_ALL_OBJECTS
I have a polymorphic structure that looks like:
@JsonTypeInfo(
use = JsonTypeInfo.Id.CLASS,
property = "class",
defaultImpl = Dog.class
)
public interface Pet {}
public class Dog implements Pet {}
and i have a generator config
JacksonModule jacksonModule = new JacksonModule(
JacksonOption.RESPECT_JSONPROPERTY_ORDER,
JacksonOption.RESPECT_JSONPROPERTY_REQUIRED,
JacksonOption.FLATTENED_ENUMS_FROM_JSONPROPERTY
);
SchemaGeneratorConfigBuilder configBuilder = new SchemaGeneratorConfigBuilder(MAPPER,
SchemaVersion.DRAFT_2020_12, new OptionPreset(
Option.SCHEMA_VERSION_INDICATOR,
Option.ADDITIONAL_FIXED_TYPES,
Option.FLATTENED_ENUMS,
Option.FLATTENED_OPTIONALS,
Option.VALUES_FROM_CONSTANT_FIELDS,
Option.PUBLIC_NONSTATIC_FIELDS,
Option.NONPUBLIC_NONSTATIC_FIELDS_WITH_GETTERS,
Option.ALLOF_CLEANUP_AT_THE_END,
Option.DEFINITIONS_FOR_ALL_OBJECTS
)).with(jacksonModule);
which is almost equivalent to OptionPreset.PLAIN_JSON except i added DEFINITIONS_FOR_ALL_OBJECTS.
when i try to generate a schema for this class. I get an exception:
SchemaDefinitionNamingStrategy of type CleanSchemaDefinitionNamingStrategy produced duplicate keys because it appears to have tried to create 2 definitions for Pet, one through DEFINITIONS_FOR_ALL_OBJECTS and one through Jackson polymorphic types lookup. I would like to be able to generate definitions for all my objects, this includes those found by Jackson. Is this possible?
@Joe102192 @CarstenWickner
Any updates on this? I'm having the same problem.
Hi @RyanMoser and @Joe102192,
I cannot reproduce the exception myself. Can you perhaps provide a complete reproducible example?
It seems like you're not using the standard schema definition naming strategy, but one that overrides the adjustDuplicateNames() method without properly adjusting the names then. The default implementation would just make these Pet-1 and Pet-2 without throwing an error.
If you don't like the default numeric indexes, perhaps give this one a try?
configBuilder.forTypesInGeneral().withDefinitionNamingStrategy(new DefaultSchemaDefinitionNamingStrategy() {
@Override
public void adjustDuplicateNames(Map<DefinitionKey, String> subschemasWithDuplicateNames, SchemaGenerationContext generationContext) {
// the definition that does not ignore any custom definition provider is the default/wrapper definition
List<Map.Entry<DefinitionKey, String>> defaultDefinitions = subschemasWithDuplicateNames.entrySet().stream()
.filter(entry -> entry.getKey().getIgnoredDefinitionProvider() == null)
.collect(Collectors.toList());
if (defaultDefinitions.size() == 1) {
Map.Entry<DefinitionKey, String> wrapperEntry = defaultDefinitions.get(0);
wrapperEntry.setValue(wrapperEntry.getValue() + "-Wrapper");
return;
}
// if there are multiple default definitions, that means there are multiple classes with the same name in different packages
super.adjustDuplicateNames(subschemasWithDuplicateNames, generationContext);
}
});
I did spend a couple of hours looking into avoiding the second definition, but I always ran into situations where my changes caused unresolved circular references or the resulting schema was utterly wrong.
The fact that there are two (nested) definitions for the same type is due to the way I realized the jackson subtype resolution. That "just" means the definition keys are not as clean/human-friendly as one might prefer them to be. But technically, the produced schema is valid.
As I cannot reproduce the error and didn't make any meaningful progress on avoiding the two separate definitions in the first place, I'm somewhat stuck.
Again: a complete reproducible example would be appreciated.
I'm working on improving the subtype handling somewhat. However I now believe the most likely cause for the duplicate key error is the fact that you have two classes with the same simple class name in your model hierarchy (in different packages). You may have to configure another naming strategy then, that considers the package name as well.
Closing this as "Cannot Reproduce" since no reproducible example was provided.
Happy to reopen this in case an example project is being provided.
I did spend a couple of hours looking into avoiding the second definition, but I always ran into situations where my changes caused unresolved circular references or the resulting schema was utterly wrong.
Would be good if this was added explicitly to the docs. Took a while going round in circles to realise the duplicate names with (-1 & -2) was intended behaviour when using Jackson with all references enabled.
Hi @annanyearian
I'll try to describe it more clearly in the documentation going forward.
Did you also try JacksonOption. INLINE_TRANSFORMED_SUBTYPES?
That was my last attempt in reducing those extra definitions and might be your best bet here.
I'm open to a PR for improving the documentation as well. 😉
Hi @CarstenWickner
Thanks! Using INLINE_TRANSFORMED_SUBTYPES mostly resolved the issue for me :)
I did run into the stackoverflow issue mentioned in #340. Though for now I've just disabled SKIP_SUBTYPE_LOOKUP and fix in post processor. If I have time in the future will try to put together a minimal test case. Probably because one of the classes is a hierarchy structure with children of it's own type.
A separate minor issue, it seems to not follow ENUM_KEYWORD_FOR_SINGLE_VALUES, but always uses const.