1
votes

I have been testing swagger in order to use it as the defacto documentation for my api service. I am using hibernate for the persistence layer and every response is bound to an entity. The problem is that those entities have dependencies with other entities and I would like to orchestrate swagger to not show those entities when I pass the object in the @ApiOperation response. The only think I could find on line is this link from the github page.

I have tried doing this:

String emptyJSON = "{}";
OverrideConverter addressConverter = new OverrideConverter();
addressConverter.add(User.class.getCanonicalName(), emptyJSON);
ModelConverters.addConverter(addressConverter, true);

but I am getting this error:

org.json4s.package$MappingException: Did not find value which can be converted into java.lang.String

Is there a way to avoid serializing some classes when I find them as fields or in lists and how?

2
If you're using Jackson, can you annotate them with @JsonIgnore? - Kelsey Gilmore-Innis

2 Answers

1
votes

Ok so it appears that you can use

 OverrideConverter sessionConverter = new OverrideConverter();
 sessionConverter.add(Session.class.getName(), emptyJSON);
 ModelConverters.addConverter(sessionConverter, true);

For globally avoiding serialization. Also if you need to avoid inner objects from serializing then just change all the getter methods to something else, seems like swagger is using this the getters and not reflections which sucks in my opinion. Hope that helps

0
votes

I have a similar issue, but I don't want to JsonIgnore all the inner entities and values on my large entity because sometimes I do want to serialize the whole object, But if my entity is used in a json response I have my own annotation to only serialize the id.

e.g. My entity is:

class Entity {
     Integer id;
     SubEntity1 subEntity1;
     SubEntity2 subEntity2;
     List<SubEntity3> subEntity3s;
     ...
}

If I return an Entity in a response Dto, I have the option to annotate that Entity object to only serialize the id, this helps to clear up a lot of clutter in the response.

e.g. ResponseDto is:

class ResponseDto {
    @JsonSerialize(using = EntityIdSerializer.class)
    Entity entity;
    ...
}

So if I get the dto in, say, Postman, the repsone looks like this:

{
    entity: { id: 1},
    ...
}

My problem is that swagger doesn't recognize this so the documentation serializes the whole entity, which looks ugly. In my actual case the entity that I am serializing can be more than 300 lines of json.

How could I configure swagger with the same @JsonSerialize feature?