Forum Discussion
priyapitaliya
New Contributor
I am using 1.5.22
frantuma
5 years agoStaff
In scenarios like the one described (multiple `JsonRootName` with same value, and `ApiModel` annotations applied, there's indeed a bug preventing `ApiModel` annotation to be considered first. This has been addressed in PR #3292 , therefore in latest snapshot and from upcoming release `1.5.24` such scenario would result in the expected outcome. With eariler version the same can be achieved providing a custom converter extending `ModelResolver` and overriding `_findTypeName`:
class CustomSchemaNameResolverConverter extends ModelResolver { public CustomSchemaNameResolverConverter(ObjectMapper mapper) { super(mapper); } protected String _findTypeName(JavaType type, BeanDescription beanDesc) { // First, handle container types; they require recursion if (type.isArrayType()) { return "Array"; } if (type.isMapLikeType()) { return "Map"; } if (type.isContainerType()) { if (Set.class.isAssignableFrom(type.getRawClass())) { return "Set"; } return "List"; } if (beanDesc == null) { beanDesc = _mapper.getSerializationConfig().introspectClassAnnotations(type); } final ApiModel model = type.getRawClass().getAnnotation(ApiModel.class); if (model != null && StringUtils.isNotBlank(model.value())) { return _typeNameResolver.nameForType(type); } PropertyName rootName = _intr.findRootName(beanDesc.getClassInfo()); if (rootName != null && rootName.hasSimpleName()) { return rootName.getSimpleName(); } return _typeNameResolver.nameForType(type); } }
and registering it:
ModelConverters.getInstance().addConverter(new CustomSchemaNameResolverConverter(Json.mapper()));
Related Content
Recent Discussions
- 17 hours agocelly
- 19 hours agoIchigo
- 20 hours agoBartMadeBotBad