Rob McFeely February 2016

Swagger Custom Model

Im using swagger and spring boot to automatically generate API docs for my objects. However I'm dynamically adding fields to my json reponse using @jsonAnyGetter @jsonAnySetter. Such as

public class SomeResponse {

    //Standard part picked up by swagger
    public String field1;
    public String field2;

    //Dynamic Part not picked up by Swagger
    private HashMap dynamicFields = new HashMap();

    @JsonAnySetter
    public final void putDynamicField(String key, Object value){
        dynamicFields.put(key, value);
    }

    @JsonAnyGetter
    public final HashMap getDynamicFields(){
        return dynamicFields;
    }
}

Produces:

{
   "field1":""
   "field2":""
}

NB when the dynamic fields appear the magic of jackson make them appear at the same level at field1/2 :) HOwever Swagger doesnt pick them up cause they appear at run-time obvioulsy

Can anyone tell me how I can update the generated Swagger Model or Model Schema description to add custom fields description of sorts of my own naming? Something like:

{
   "field1":""
   "field2":""

   "dynamicField1": {}
   "dynamicFieldN": {}

}

Answers


fehguy February 2016

The @JsonAnyGetter and @JsonAnySetter are not supported by Swagger. You'll have to have concrete model definitions, or configure a custom model converter to add run-time fields to your models.

Post Status

Asked in February 2016
Viewed 1,710 times
Voted 14
Answered 1 times

Search




Leave an answer