2

I have the following piece of code and swagger is not showing up the way i expect on swagger ui. I'm using annotations to build the swagger definition. I tried using @API and @JsonIgnore. Both did not work for me.

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import io.swagger.annotations.Api;


@JacksonXmlRootElement(localName = "traction")
@Api(hidden = true)
public class Traction
{
  private JsonNode traction;

  public JsonNode getTraction()
  {
    return traction;
  }

  public void setTraction(final JsonNode traction)
  {
    this.traction = traction;
  }

}

Swagger definition gets displayed as follows -

"tractionParent": {
    "traction": {
      "array": false,
      "null": false,
      "float": false,
      "containerNode": false,
      "missingNode": false,
      "nodeType": "ARRAY",
      "valueNode": false,
      "object": false,
      "pojo": false,
      "number": false,
      "integralNumber": false,
      "short": false,
      "int": false,
      "long": false,
      "double": false,
      "bigDecimal": false,
      "bigInteger": false,
      "textual": false,
      "boolean": false,
      "binary": false,
      "floatingPointNumber": false
    }
  }

I need it to show up as

"tractionParent": {
    "traction": {
     }
 }
user2352834
  • 69
  • 1
  • 2
  • 8
  • Swagger shows all fields of a type. JsonNode looks like that inside, thus swagger 'expects' you to set them. If you really need just empty braces, then make traction of type Object. – phil_g May 15 '17 at 20:35

2 Answers2

0

You have taken 'traction' variable type JsonNode that why you are getting all properties of Class com.fasterxml.jackson.databind.JsonNode. For more detail you visit on link JsonNode Class to view the Source code

0

I faced the same problem recently and solved it by registering a custom ModelConverter which maps a JsonNode to the free form object for swagger

class JsonNodeProperty extends AbstractProperty {
    protected boolean additionalProperties = true;

    public JsonNodeProperty() {
        setType("object");
    }

    public boolean isAdditionalProperties() {
        return additionalProperties;
    }

    public void setAdditionalProperties(boolean additionalProperties) {
        this.additionalProperties = additionalProperties;
    }
}

class JsonNodeModelConverter implements ModelConverter {
    @Override
    public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator<ModelConverter> chain) {
        JavaType javaType = Json.mapper().constructType(type);
        if (javaType != null) {
            Class<?> clazz = javaType.getRawClass();
            if (JsonNode.class.isAssignableFrom(clazz)) {
                return new JsonNodeProperty();
            }
        }
        if (chain.hasNext()) {
            return chain.next().resolveProperty(type, context, annotations, chain);
        } else {
            return null;
        }
    }

    @Override
    public Model resolve(Type type, ModelConverterContext context, Iterator<ModelConverter> chain) {
        if (chain.hasNext()) {
            return chain.next().resolve(type, context, chain);
        } else {
            return null;
        }
    }
}

You can register the custom converter via

ModelConverters.getInstance().addConverter(new JsonNodeModelConverter());
Nico
  • 3,542
  • 24
  • 29
  • I think I have the same issue. Where should I get ModelConverter? Is this from springfox-swagger2? what version ? – user3441604 Nov 11 '19 at 16:00
  • @user3441604 I used the converter from swagger core. Documentation: https://github.com/swagger-api/swagger-core/wiki/overriding-models – Nico Nov 12 '19 at 10:27