Model Schema field ordering

1,432 views
Skip to first unread message

James Cowhen

unread,
Sep 24, 2013, 2:32:35 AM9/24/13
to swagger-sw...@googlegroups.com
I'm running Play 2.1 Scala and I have my project integrated with Swagger 1.2.4 mostly smoothly. However I noticed that my model and datatypes are reflected in an odd order. 

For example, here is one of my case class:

case class Device(deviceId: String, platform: String)

object Device {
    implicit val format = Json.format[Device]
}



My Controller is simply:

@ApiOperation(value = "Create user", notes = "Registers a user via device", responseClass = "models.response.User")
    @ApiParamsImplicit(Array(
        new ApiParamImplicit(name = "body", value = "Created user object", required = true, dataType = "models.request.device", paramType = "body")))
    def register = Action(parse.json) { implicit request =>
       //do stuff
    }



I assumed swagger would return a json of deviceId and platform in that order, but instead it's platform and deviceId. The ordering gets even more random with more fields in a class. 

Is there something I'm doing incorrectly?

tony tam

unread,
Sep 24, 2013, 9:23:36 AM9/24/13
to swagger-sw...@googlegroups.com
Hi, reflection does not guarantee order, ever--this was addressed in swagger-core 1.3 by adding a position annotation for each field.  I don't think it can be backported to swagger 1.2.4 easily though:


case class DomesticAnimal (
  @(ApiModelProperty @field)(value = "name of animal", position = 1) name: String,
  @(ApiModelProperty @field)(value = "animals are safe for children", position = 2) safeForChildren: Boolean,
  @(ApiModelProperty @field)(value = "date added", position = 3) date: java.util.Date)

It sounds like one of our users has mostly ported swagger-core 1.3 into play2, I'll follow up to see if that can be committed shortly.
Reply all
Reply to author
Forward
0 new messages