Rob McFeely
Rob McFeely

Reputation: 3053

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": {}

}

Upvotes: 2

Views: 4612

Answers (1)

fehguy
fehguy

Reputation: 6824

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.

Upvotes: 1

Related Questions