convert complex datatype to string datatype

51 views
Skip to first unread message

sat

unread,
Apr 21, 2014, 2:06:48 PM4/21/14
to swagger-sw...@googlegroups.com
Hi,

I've a Track model with a property URI.  The URI was displaying unwanted properties. So I added the below piece of code.

String jsonString = "{" +
                "  \"id\": \"URI\"," +
                "  \"properties\": {" +
                "    \"value\": {" +
                "      \"required\": true," +
                "      \"description\": \"A URL to a resource\"," +
                "      \"type\": \"string\"" +
                "    }" +
                "  }" +
                "}";
        OverrideConverter converter = new OverrideConverter();
        converter.add("java.net.URI", jsonString);
        ModelConverters.addConverter(converter, true);
       
The above code removed unwanted properties and displays the json like given below.
"models": {
    "Track": {
        "id": "Track",
        "properties": {
            "title": {
                "type": "string"
            },
            "singer": {
                "type": "string"
            },
            "lane": {
                "$ref": "Lane"
            },
            "uri": {
                "$ref": "URI"
            },
            "date": {
                "type": "string",
                "format": "date-time"
            }
        }
    },
  ..
    "URI": {
        "id": "URI",
        "required": [
            "value"
        ],
        "properties": {
            "value": {
                "type": "string",
                "description": "A URL to a resource"
            }
        }
    }
   
What I need is instead of
 "uri": {
                "$ref": "URI"
            },
I want the json to display URI like this

 "uri": {
                "type": "string",
            },
           
How can I achieve this? Can anyone help me with this?

This is my model class.
public class Track {

    private String title;
    private String singer;
    @ApiModelProperty(access = "internal")
    private byte[] data;
    private Lane lane;
    private URI uri;
    private Date date;

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSinger() {
        return singer;
    }

    public void setSinger(String singer) {
        this.singer = singer;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public Lane getLane() {
        return lane;
    }

    public void setLane(Lane lane) {
        this.lane = lane;
    }

    public URI getUri() {
        return uri;
    }

    public void setUri(URI uri) {
        this.uri = uri;
    }

}

tony tam

unread,
Apr 21, 2014, 11:59:59 PM4/21/14
to swagger-sw...@googlegroups.com
I believe you should override the definition for Track instead of URI.  Then you can set URI to be a string inside the Track object.  Does that make sense?

sat

unread,
Apr 22, 2014, 9:26:31 AM4/22/14
to swagger-sw...@googlegroups.com
Hi Tony,

I tried that at first. Though the URI works fine as I expected, but the Lane model reference is not getting generated. This is how the json is generated. The lane reference is empty.
..
"lane": {
                "$ref": ""
            },
            "uri": {
                "type": "string"
            },
..
Reply all
Reply to author
Forward
0 new messages