How to replace Swagger's enum with a link to a resource?

129 views
Skip to first unread message

Niccolò Giulio Rossetti

unread,
Jun 2, 2016, 7:49:41 AM6/2/16
to Swagger

I am working with Swagger (API v. 1.5.8) to document my Java/dropwizard code.


I know from the documentation that I can annotate my POJOs like this:


@ApiModelProperty(value = "pet status in the store", allowableValues = "available,pending,sold")
public String getStatus() {
 
return status;
 
}

to produce something like:

"properties": {
       
...,
       
"status": {
         
"type": "string",
         
"description": "pet status in the store",
         
"enum": [
           
"available",
           
"pending",
           
"sold"
         
]
       
}
     
}


Image now to implement the method:


@ApiModelProperty(value = "pets in the store")
public Set<String> getPets() {
   
return pets;
}

which returns a list of pets available in the store. For example, one day it could be

["cats", "dogs", "songbirds"]

and then just
["cats", "dogs"]

when the songbirds get sold out.

My API would in fact have an endpoint to fetch the list of pets:

http://petShop.foo/pets

Instead of using

allowableValues = "cats, dogs, songbirds"

I would like to specify with a Swagger annotation that that field must contain a value returned by the given endpoint. That is, something like:

@ApiModelProperty(value = "pets in the store", allowableValues = "/pets")
public Set<String> getPets() {...}

This in order to allow my client/front-end to know which values can be use when making a request to, for example, buying a pet online. Exactly how I could do if I had

"enum": ["cats", "dogs", ..]


Is it possible to accomplish something like this with the Swagger annotation?
If not, can I add a custom annotation to Swagger to do the same?


Thanks,
Niccolo`

Niccolò Giulio Rossetti

unread,
Jun 3, 2016, 9:28:32 AM6/3/16
to Swagger
Alternatively, I could associate a Model to an operation/path by using a tag.

However I have been unable to insert tags in a model.
Nor it seems I can directly customize a Model with @Extension (the documentations says it is not supported for @ApiModel or @ApiModelProperty: "t is currently supported within the @ApiOperation, @Info and @Tag annotations").

tony tam

unread,
Jun 7, 2016, 11:48:19 AM6/7/16
to swagger-sw...@googlegroups.com
It’s currently not supported—you can definitely override the listing resource:


And mutate the swagger definition on each request, which would allow you to update the enum values.

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages