cancel
Showing results for 
Search instead for 
Did you mean: 

Swagger UI, @ApiResponse doesn't give correct response as a examle value.

New Contributor

Swagger UI, @ApiResponse doesn't give correct response as a examle value.

I have implemented swagger UI in my application. Everything is working fine but the issue is,
consider the situation in my API I have different request and response object, but both object's class is annotated with the same json root element name (Ex: @JsonRootName("user")).
For this reason in my swagger UI it gives me same example value in Parameters body and in Responses body. If i remove this @JsonRootName tag from any one class than swagger UI shows me correct example value, but for my application i can't remove this annotation from any class and even can't change the value of it.
I tried many option available on internet but can't resolve my problem. Please help me to resolve this.
Please refer the attached screens.

 

Thanks, Waiting for your response.

7 REPLIES 7
Moderator

Re: Swagger UI, @ApiResponse doesn't give correct response as a examle value.

Can you provide the openapi.json or swagger.json you get from your application?


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
New Contributor

Re: Swagger UI, @ApiResponse doesn't give correct response as a examle value.

Hi @RonRatovsky ,

Thank you for your response. Please find my json file.

Moderator

Re: Swagger UI, @ApiResponse doesn't give correct response as a examle value.

If I follow everything you shared correctly, it looks like that by overriding the JSON model name, you end up having two separate objects with the same name, and one ends up canceling the other when it's translated to the definition. You'd have to give the models a distinct name, otherwise it'll keep happening.


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
New Contributor

Re: Swagger UI, @ApiResponse doesn't give correct response as a examle value.

Yes agreed,
But is there any way to resolve this. As i can not change json root name value.

Moderator

Re: Swagger UI, @ApiResponse doesn't give correct response as a examle value.

Which version of swagger-core do you use?


Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
New Contributor

Re: Swagger UI, @ApiResponse doesn't give correct response as a examle value.

I am using 1.5.22

Staff

Re: Swagger UI, @ApiResponse doesn't give correct response as a examle value.

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()));

 

New Here?
Join us and watch the welcome video:
Watch the new Interview