Search code examples
javajsonhashmapapache-camelswagger

Configure Pojo with Map attribute doesnt' generate "additionalProperties" using Swagger 1.5


I have implemented some endpoints using Spring Boot and Apache Camel and I'm trying to document them with camel-swagger-java and generating a client library with swagger-codegen later on. The issue is that one of the attributes of my model is a HashMap and I'm trying to configure the pojo in a way that swagger could generate the additionalProperty but no luck so far.

    @JsonInclude(Include.NON_NULL)
    public class TaskDataResultResponse {

        private String testing;

        @JsonInclude(Include.NON_NULL)
        @ApiModelProperty(dataType = "Map[String,String]")
        private Map<String, String> properties;

        @JsonAnyGetter
        public Map<String, String> getProperties() {
            return properties;
        }

        @JsonInclude(Include.NON_NULL)
        @JsonAnySetter
        public void setProperties(String name, String value) {
            properties.put(name, value);
        }

        public String getTesting() {
            return testing;
        }

        public void setTesting(String testing) {
            this.testing = testing;
        }
    }

I've tried everyting: Adding the JsonAnySetter and Getter annotations, Adding the ApiModelProperty swagger anotation, even extend the class from HashMap instead of having a HashMap attribute but nothing seems to work:

Swagger schema with no "additionalProperties" in it

I've read a lot in swagger-core github repo, in stack overflow and searching the web and so far nothing. I know lots of stuff of how configure a swagger schema file by adding this additional property but my goal is to generate that automatically from a pojo.

I'm using the latest Spring Boot (2.4.3, but I also tested with previous versions as well) and camel-swagger-java 3.7.3 (but also tested wth 2.25.2)


Solution

  • After a lot of investigation I found the issue in my code: I was mixing up annotations. After removing ApiModelProperty, JsonAnyGetter and setter annotations the schema file finally got generated correctly.

    Also, related to this issue, I found a way to implement the same logic but with a list of hashmaps, because i also needed the additionalProperties attribute to be generated in the swagger schema for elements on a list.

    List of Hashmaps in an attribute

    Swagger scheme with additionalProperties on a list

    Finally I have generated a client (using swagger codegen) and the information is being unmarshalled correctly, for both formdata and datagridData attributes.