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

Add support for Maps / additionalProperties

Open newtechfellas opened this issue 12 years ago • 47 comments

How to specify the swagger documentation annotation for generating model schema for Map attribute in a Java bean?

//groovy code
class Foo {
  int id
  String description
  Map<String, Bar> targets
  //few other attributes
}

For targets attribute, swaggerui shows 1 line but does not give the model schema for Bar class.

targets (Map[string,Bar])

How do I get Bar class Model schema as well in UI? Am using latest swagger library ('com.wordnik:swagger-jersey-jaxrs_2.10:1.3.8')

newtechfellas avatar Sep 03 '14 19:09 newtechfellas

Swagger currently doesn't support maps, but the new version of it which is really close is going to. On Sep 3, 2014 10:49 PM, "newtechfellas" [email protected] wrote:

Reopened #558 https://github.com/wordnik/swagger-ui/issues/558.

— Reply to this email directly or view it on GitHub https://github.com/wordnik/swagger-ui/issues/558#event-160784249.

webron avatar Sep 03 '14 19:09 webron

hmm thank you for the clarification. Any tentative date of new version with Map support?

newtechfellas avatar Sep 03 '14 20:09 newtechfellas

About a week.

On 3 September 2014 23:57, newtechfellas [email protected] wrote:

hmm thank you for the clarification. Any tentative date of new version with Map support?

— Reply to this email directly or view it on GitHub https://github.com/wordnik/swagger-ui/issues/558#issuecomment-54365803.

webron avatar Sep 04 '14 06:09 webron

Is this available now? If yes. any sample documentation on which swagger annotations to add for Map attribute?

newtechfellas avatar Sep 23 '14 15:09 newtechfellas

This is more of a swagger-core issue, but you can try using 1.5.0-SNAPSHOT. Keep in mind it's fresh so there may be bugs with it.

On 23 September 2014 18:48, newtechfellas [email protected] wrote:

Is this available now? If yes. any sample documentation on which swagger annotations to add for Map attribute?

— Reply to this email directly or view it on GitHub https://github.com/wordnik/swagger-ui/issues/558#issuecomment-56541431.

webron avatar Sep 23 '14 16:09 webron

Yes, you can use the following annotations in an operation:

@ApiOperation(
  value = "",
  notes = "",
  response = ResponseMessage.class,
  responseContainer = "Map")

For a model:

https://github.com/wordnik/swagger-core/blob/develop_2.0/modules/swagger-core/src/test/scala/models/Person.java#L9

will produce this:

{
  "Person": {
    "properties": {
      "id": {
        "type": "integer",
        "format": "int64"
      },
      "firstName": {
        "type": "string"
      },
      "address": {
        "$ref": "Address"
      },
      "properties": {
        "type": "object",
        "additionalProperties": {
          "type": "string"
        }
      },
      "birthDate": {
        "type": "string",
        "format": "date-time"
      },
      "float": {
        "type": "number",
        "format": "float"
      },
      "double": {
        "type": "number",
        "format": "double"
      }
    }
  }
}

fehguy avatar Sep 23 '14 16:09 fehguy

It did not work. I tried with latest com.wordnik:swagger-jersey-jaxrs_2.10:1.3.10 version. Swagger doc still shows targets (Map[string,Bar]) for attribute of type Map and no expansion for Bar class properties. (Referring to Foo class in my question) Also tried the responseContainer attribute for @ApiOperation annotation. No difference with this either.

@ApiOperation(value = "get foo details for person id and foo id", response = Foo, responseContainer = "Map")

newtechfellas avatar Sep 24 '14 16:09 newtechfellas

You need to be using 1.5.0-SNAPSHOT as the dependency.

fehguy avatar Sep 24 '14 17:09 fehguy

the javadoc says 'Valid values are "List", "Array" and "Set"'.. support for Map is new?

lluu avatar Sep 24 '14 20:09 lluu

The javadocs are based on the 1.3.X release, not updated to 1.5.X yet.

webron avatar Sep 24 '14 20:09 webron

where would I find 1.5.0-SNAPSHOT version? I checked http://repo1.maven.org/maven2/com/wordnik/ but did not find it.

newtechfellas avatar Sep 25 '14 14:09 newtechfellas

I don't think the SNAPSHOT has been pushed yet. You can clone the repository and built it locally.

webron avatar Sep 25 '14 14:09 webron

There is snapshot in sonatype. maven.org doesn't do snapshots, I believe.

Add this dependency to your pom.xml:

  <repositories>
    <repository>
      <id>sonatype-snapshots</id>
      <url>https://oss.sonatype.org/content/repositories/snapshots</url>
      <snapshots>
        <enabled>true</enabled>
      </snapshots>
    </repository>
  </repositories>

fehguy avatar Sep 26 '14 18:09 fehguy

Didn't mean to close it, will when it's supported in the UI

fehguy avatar Sep 26 '14 18:09 fehguy

Any estimations when this will be implemented? Maps are ubiquitous in our api

mjgolinski avatar Dec 05 '14 10:12 mjgolinski

@mjgolinski - are you using Swagger 2.0?

webron avatar Dec 05 '14 10:12 webron

Yes

mjgolinski avatar Dec 05 '14 11:12 mjgolinski

to be clear @mjgolinski maps are supported in the 1.5.0-SNAPSHOT version of swagger-jaxrs. In your response, just use containerType="Map". I think they're not complete in the UI, though.

fehguy avatar Dec 05 '14 17:12 fehguy

@fehguy I need specifically swagger-ui support or at least estimates about when it will be implemented. Should I open a new ticket for it (I thought this one was about it)?

mjgolinski avatar Dec 07 '14 16:12 mjgolinski

Hi - I also need it on the UI for submitting a map of strings... I checked out the ui and it doesn't seem to support it yet .. any timeline for this?

rosselet avatar Dec 09 '14 01:12 rosselet