mercredi 10 juin 2015

Make Model schema user-input dependent

I am doing an adapter for a REST API. I've used model schema for the POST and PUT method's body. @RequestBody Model requestBody at the adapter.

Now, I encountered body with fields that are optional and some that requires an array.

Swagger UI body input:

{
    "field1"         : "input1",
    "field2Optional" : "input2-optional",
    "fieldArray"     : [
        { "field2a"                  :  "input1a" },
        { "field2b-userAddition"     :  "input2b-userAddition " }
    ]

}

(1) How can I set fieldOptional ( inside getMethod() ) to have an empty string value if the user hasn't specified a value for it?

"field2Optional" : "input2-optional"

(2) How can I get the fields the user added in the Swagger UI body?

{ "field2b-userAddition"     :  "input2b-userAddition " }

..having this current Model.groovy:

Model1.groovy

@XmlElement
private String field1 = ''

@XmlElement
private Model2 fieldOptional = new Model2()

@XmlElement
private ArrayList<Model3> fieldArray = new ArrayList<>( Arrays.asList(new Model3()) )

//called by the adapter to process the input in swagger UI
//maximum possible request `body`
public getMap() {
   [ 
     field1        : field1,
     fieldOptional : fieldOptional.getMap(),
     fieldArray    : fieldArray.get(0).getMap()
   ]
}

Aucun commentaire:

Enregistrer un commentaire