swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

Incorrect Response with List Object

Open PeaceInColour opened this issue 8 years ago • 3 comments

Q A
Bug or feature request? Bug
Which Swagger-Core version? 1.5.18
Which Java version? 1.8
Which JAX-RS framework & version? RESTEasy 3.0.19 with JAX-RS 2.0

I have a method which returns an object which inherits from ArrayList. Swagger Core converts the response to be an array instead of an object which is an array. For example:

Interface:

public interface SomeInterface {
  @GET
  @ApiOperation(...)
  FooList listFoo();
}

FooList:

public class FooLists extends ArrayList<Foo> {
}

will have a generated response that looks like:

"200": {
  "description": "successful operation",
  "schema": {
  "type": "array",
      "items": {
          "$ref": "#/definitions/Foo"
        }
    }
}

which has no reference to FooList. Foo is properly documented in the generated OpenAPI file but FooList is not included at all since the class isn't used anywhere else. While this isn't really an issue as far as documentation goes (FooList is basically just an array of Foo objects) I was hoping to use the generated OpenAPI file for generating SDKs with Codegen. Since the server's response will be structured like:

{
  "FooList" : [
    { 
    }
  ]
}

you end up with deserialization errors. I've tried hardcoding the response object in @ApiOperation but the result doesn't change. I can override the response reference with @ApiOperation but since FooList isn't being used anywhere else the reference isn't included in the definitions section of the OpenAPI file.

PeaceInColour avatar Mar 05 '18 21:03 PeaceInColour

I started to look through the code base and found the bug. In ModelResolver.java from lines 127 - 155 there is this chunk of code:

        Property property = null;
        if (propType.isContainerType()) {
            JavaType keyType = propType.getKeyType();
            JavaType valueType = propType.getContentType();
            if (keyType != null && valueType != null) {
                property = new MapProperty().additionalProperties(context.resolveProperty(valueType, new Annotation[]{}));
            } else if (valueType != null) {
                Property items = context.resolveProperty(valueType, new Annotation[]{});
                // If property is XmlElement annotated, then use the name provided by annotation | https://github.com/swagger-api/swagger-core/issues/2047
                if(annotations != null && annotations.length > 0) {
                    for (Annotation annotation : annotations) {
                        if(annotation instanceof XmlElement) {
                            XmlElement xmlElement =   (XmlElement)annotation;
                            if(xmlElement != null && xmlElement.name() != null && !"".equals(xmlElement.name()) && !"##default".equals(xmlElement.name())) {
                                Xml xml = items.getXml() != null ? items.getXml() : new Xml();
                                xml.setName(xmlElement.name());
                                items.setXml(xml);
                            }
                        }
                    }
                }
                ArrayProperty arrayProperty =
                        new ArrayProperty().items(items);
                if (_isSetType(propType.getRawClass())) {
                    arrayProperty.setUniqueItems(true);
                }
                property = arrayProperty;
            }
        }

Up until this point propType has the correct information in it which looks like: [collection type; class FooList, contains [simple type, class Foo]]. However, the above chunk is only checking for the key type (for Maps) and the content type. The content type (valueType) will be set to Foo and the else if will set the Property object to be an ArrayProperty object. The FooList data is dropped completely creating a response object of a Foo array.

PeaceInColour avatar Mar 07 '18 17:03 PeaceInColour

I've been playing around with the code and have gotten some results (although my code at the moment is a bit hacky). I'm not 100% sure though what the end result should look like. As in, what's the correct formatting in the OpenAPI file for the response?

This is the best format I've got so far for displaying an object that inherits from ArrayList is this:

"responses": {
                    "200": {
                        "schema": {
                            "type": "object",
                            "title": "FooList",
                            "properties": {
                                "FooList": {
                                    "type": "array",
                                    "items": {
                                        "$ref": "#/definitions/Foo"
                                    }
                                }
                            }
                        }
                    },

However, this format still doesn't accurately represent an object which inherits from ArrayList. Would using allOf be the correct way to go? I've only seen examples of allOf being used with definitions that can be referenced instead of Java classes like ArrayList.

PeaceInColour avatar Mar 08 '18 22:03 PeaceInColour

Closing this issue since it isn't worth fixing right now. Swagger Core could handle this issue better but the 2.0 spec also doesn't have a perfect way to document an object which inherits an ArrayList object. The best solution is to use composition instead of inheritance as both Swagger Core doesn't have any issues with composition and it makes the code cleaner.

PeaceInColour avatar Mar 19 '18 19:03 PeaceInColour