Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ApiModelProperty required field seems to get ignored #542

Closed
avdkamp opened this issue Apr 28, 2014 · 5 comments
Closed

ApiModelProperty required field seems to get ignored #542

avdkamp opened this issue Apr 28, 2014 · 5 comments

Comments

@avdkamp
Copy link

avdkamp commented Apr 28, 2014

We noticed that the models in Swagger UI is always showing optional behind the datatype;

Model {
       variable0 (string, optional),
       variable1 (string, optional),
       variable2 (string, optional),
       variable3 (integer, optional)
}

Our model looks like this;

@ApiModel(value = "Model", description = "Model description")
public class Model {
       @ApiModelProperty(value = "variable0", dataType = "java.lang.String", required = true)
       private String variable0;
       @ApiModelProperty(value = "variable1", dataType = "java.lang.String", required = true)
       private String variable1;
       //required not set because default = false
       @ApiModelProperty(value = "variable2", dataType = "java.lang.String")
       private String variable2;
       @ApiModelProperty(value = "variable3", dataType = "java.lang.Integer", required = true)
       private Integer variable3;
...
}

The json that gets returned from api-docs/my-controller;

{
  "apiVersion" : "1.0",
  "swaggerVersion" : "1.2",
  "basePath" : "http://localhost/api-docs",
  "resourcePath" : "/my-controller",
  "produces" : [ "application/json" ],
  "consumes" : [ "application/json" ],
  "apis" : [ {
    "path" : "/model",
    "description" : "description",
    "operations" : [ {
      "method" : "POST",
      "summary" : "Create Models",
      "notes" : "This endpoint allows the creation of Models",
      "type" : "integer",
      "format" : "int64",
      "nickname" : "createModel",
      "produces" : [ "application/json" ],
      "consumes" : [ "application/json" ],
      "parameters" : [ {
        "name" : "model",
        "description" : "Well-formed Model",
        "defaultValue" : "",
        "required" : true,
        "type" : "Model",
        "paramType" : "body"
      } ],
      "responseMessages" : [ {
        "code" : 401,
        "message" : "Authorization failed"
      }, {
        "code" : 500,
        "message" : "Server error"
      }, {
        "code" : 503,
        "message" : "Server temporary error"
      } ],
      "deprecated" : "false"
    } ]
  } ],
 "models" : {
    "Model" : {
      "id" : "Model",
      "description" : "",
      "extends" : "",
      "properties" : {
        "description" : {
          "type" : "string"
        },
        "variable0" : {
          "type" : "string"
        },
        "variable1" : {
          "type" : "string"
        },
        "variable2" : {
          "type" : "string"
        },
        "variable3" : {
          "type" : "integer",
          "format" : "int32"
        }
      }
    }
...

As can be seen, the json returned is lacking any kind of field to show that the fields in the model are required. However it does show the "required" field we have defined in the method in our controller.

Now this is quite a blocker for us as some values truly are required and we would love to use this in our swagger but can't as of now. We were discussing if this was a problem caused by swagger-springmvc or by swagger-core but we are not sure which one it is.

Any help would be very much appreciated!

@fehguy
Copy link
Contributor

fehguy commented Apr 28, 2014

Hi, it seems like there are some combinations of "where" annotations live that is causing some annotations to be ignored. For example, having @xmlelement annotations seem to cause headaches for the @ApiModelProperty annotations.

Can you please post more details on your pojos? From the petstore sample, this:

@XmlRootElement(name = "Pet")
public class Pet {
  private long id;

  @ApiModelProperty(value = "pet id", required=true)
  public long getId() {
    return id;
  }

appears correct:

    "Pet": {
      "id": "Pet",
      "required": [
        "id"
      ],
      "properties": {
        "id": {
          "type": "integer",
          "format": "int64",
          "description": "pet id"
        },

@avdkamp
Copy link
Author

avdkamp commented Apr 29, 2014

@fehguy , thanks for your reply, it gave me quite an insight on how to make it work actually. From what I noticed is that the difference between our annotations is that I had put the annotations above the variable and not above the getter method.
When I changed the following code:

@ApiModel(value = "Model", description = "Model description")
public class Model {
       @ApiModelProperty(value = "variable0", required = true)
       private String variable0;
...
       public String getVariable0(){
               return variable0;
       }
...
}

to:

@ApiModel(value = "Model", description = "Model description")
public class Model {
       private String variable0;
...
       @ApiModelProperty(value = "variable0", required = true)
       public String getVariable0(){
               return variable0;
       }
...
}

it showed the required ones. To me this seems that the annotation directly above a variable does not work properly. So there is a work-around available (or this is working as intended and we were implementing it wrongly.)

the json returned looks like this now (a shorter version than i previously posted though):

"models" : {
    "Model" : {
      "id" : "Model",
      "description" : "Model description",
      "required" : [ "variable0" ],
      "extends" : "",
      "properties" : {
         "variable0" : {
          "type" : "string",
          "description" : "variable0"
        },

It also looks like that the "value = variable0" was ignored as well when putting the annotation above the variables as it now shows two extra fields in the json file;

  • required
  • description

The pojos that we have annotated are basically nothing more than shown here, just a class with variables,a getter and a setter.

@fehguy
Copy link
Contributor

fehguy commented May 2, 2014

Hi, I just wrote a unit test to see if we get the right thing from your model. This what I see:

[
  {
    "id": "AnnotatedPOJO",
    "required": [
      "variable0",
      "variable1",
      "variable3"
    ],
    "properties": {
      "variable0": {
        "type": "string",
        "description": "variable0"
      },
      "variable1": {
        "type": "string",
        "description": "variable1"
      },
      "variable2": {
        "type": "string",
        "description": "variable2"
      },
      "variable3": {
        "type": "integer",
        "format": "int32",
        "description": "variable3"
      }
    }
  }
]

So something else is going on.

What version of swagger-core are you using? Can you try 1.3.5? Are you using OSGi or something else?

@avdkamp
Copy link
Author

avdkamp commented May 5, 2014

Hi, I had send the same issue to the guys from swagger-spring-mvc and it seems that they have not implemented this yet.
As far as I can see, the version that their latest version(0.8.3), which is available on Maven, is using swagger-core 1.3.2.

The output I receive is exactly the same for me, to bad that this is more of a work around for their library. Other variables besides "required" and "value" are not working(, yet I presume.)

Thanks again for your help as it made the "required" fields on the variables work, which was the most important thing for us to have.

@fehguy
Copy link
Contributor

fehguy commented May 5, 2014

thanks for the follow-up--I'll reference the issue in swagger-spring MVC for others, and close this:

springfox/springfox#281

@fehguy fehguy closed this as completed May 5, 2014
stankevichevg added a commit to stankevichevg/swagger-core that referenced this issue Jun 20, 2014
… those fields which have public accessory method (getter)
stankevichevg added a commit to stankevichevg/swagger-core that referenced this issue Jun 21, 2014
… those fields which have public accessory method (getter)
stankevichevg added a commit to stankevichevg/swagger-core that referenced this issue Jun 21, 2014
stankevichevg added a commit to stankevichevg/swagger-core that referenced this issue Jun 21, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants