question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Map support in swagger

See original GitHub issue

I have input to my rest API below:

{ “a”:“value”, “resources”: {“key1”:“value1”, “key2”:“value2” } } When I am doing

class MyModelClass { @XmlElement(name = “resources”) @ApiModelProperty(value = “resources”) Map<String, MYClass> resources= new HashMap<String,MyClass>();

//Setters and getters for resource object. }

Now

public class MyClass{

@XmlTransient
JSONObject pushnotificationResources = new JSONObject();

public JSONObject getPushnotificationResources() {
    return pushnotificationResources;
}
public void setPushnotificationResources(JSONObject pushnotificationResources) {
    this.pushnotificationResources = pushnotificationResources;
}

}

when I am loading my application I am getting the below error: Can not instantiate value of type [simple type, class packagename.MYClass] from String value; no single-String constructor/factory method (through reference chain: com.tycoon.auth.idm.identity.model.MYModelClass[“resources”])

Please help me to accept a json object with key value pairs as part of an attribute value? like below {“resource”: {“k1”:“v1”,“k2”,“v2”}}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
fehguycommented, Jun 4, 2016

Simply override the dataType for your JSONObject value like such:

public class MyModel {
  @ApiModelProperty(value="dummyAttrib", dataType = "java.util.Map")
  public JsonNode getDummyAttrib() {
    return null;
  };
  public void setDummyAttrib(JsonNode node) {}
}

Then your definition looks like it accepts a Map<String, Object>:

{
  "MyModel" : {
    "type" : "object",
    "properties" : {
      "dummyAttrib" : {
        "type" : "object",
        "description" : "dummy",
        "additionalProperties" : {
          "type" : "object"
        }
      }
    }
  }
}
0reactions
fehguycommented, Jun 27, 2016

Hi, please take a look at the swagger-samples project for integration examples.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dictionaries, Hashmaps, Associative Arrays - Swagger
A dictionary (also known as a map, hashmap or associative array) is a set of key/value pairs. OpenAPI lets you define dictionaries where...
Read more >
Swagger: map of <string, Object> - dictionary - Stack Overflow
I need to document with Swagger an API that uses, both as input and output, maps of objects, indexed by string keys. ......
Read more >
Support Maps · Issue #244 · swagger-api/swagger-core - GitHub
Supporting maps with arbitrary key types would make swagger codegen very complicated but supporting a Map[String, Object] would be very ...
Read more >
Swagger Hashmap - SmartBear Community
Can anyone help me how to give key value pair as input in swagger, ... Swagger HashMap property type · Swagger: map of...
Read more >
OpenAPI Map
OpenAPI Specification Visual Document) aims to help you find your way in the OpenAPI Specification (fka. Swagger Specification) documentation. It has been ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found