Ignore a @JsonSubType in the JSON schema by custom annotation #347
-
Hi!, @JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes({
@JsonSubTypes.Type(value = InternalSubType.class, name = "internal-subtype"),
@JsonSubTypes.Type(value = SubType.class, name = "subtype"),
})
public interface BaseType {
}
@Value
@HideFromJsonSchemaGenerator
public class InternalSubType implements BaseType {
int field1;
int field2;
}
@Value
public class SubType implements BaseType {
int field1;
} Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
HI @roded, This is certainly possible, just not out-of-the-box. I suggest you use the JsonSubTypesResolver subtypeResolver = new JsonSubTypesResolver(jacksonOptions) {
public List<ResolvedType> lookUpSubtypesFromAnnotation(ResolvedType declaredType, JsonSubTypes subtypesAnnotation, TypeContext context) {
List<ResolvedType> subtypes = super.lookUpSubtypesFromAnnotation(declaredType, subtypesAnnotation, context);
/* remove undesired subtypes from the list */
return subtypes;
}
};
generalConfigPart.withSubtypeResolver(subtypeResolver);
fieldConfigPart.withTargetTypeOverridesResolver(subtypeResolver::findTargetTypeOverrides);
methodConfigPart.withTargetTypeOverridesResolver(subtypeResolver::findTargetTypeOverrides);
generalConfigPart.withCustomDefinitionProvider(subtypeResolver);
fieldConfigPart.withCustomDefinitionProvider(subtypeResolver::provideCustomPropertySchemaDefinition);
methodConfigPart.withCustomDefinitionProvider(subtypeResolver::provideCustomPropertySchemaDefinition); Unfortunately, I don't have the time to provide a full example in the next couple of days. |
Beta Was this translation helpful? Give feedback.
HI @roded,
This is certainly possible, just not out-of-the-box.
I suggest you use the
JacksonModule
as per your current configuration but include theJacksonOption.SKIP_SUBTYPE_LOOKUP
andJacksonOption.IGNORE_TYPE_INFO_TRANSFORM
.Then you apply its
JsonSubTypesResolver
yourself -- with a little override, e.g.