swagger model definition for POST and GET methods
swagger model definition for POST and GET methods
I am new to Swagger and open api. I am trying to create a model for an object (let us say "package" object).
For the POST method, I don't define an 'id' parameter, since I expect the back-end system to assign a new UUID for the package being created.
"name": "My Beautiful Package",
"channel": 0,
"status": 0,
"visibility": true
However, for the GET method, the response contains the id (UUID).
"id" : "afefeffbd-384fe3",
"name": "My Beautiful Package",
"channel": 0,
"status": 0,
"visibility": true,
"createdOn": "2018-Sep-08",
"createdBy"" 2234
So, my question is..,
Should I create two models, one for POST methods and one for GET method?
If I define the 'id' field as optional, it might still get shown in the api as one of the optional parameters. This I don't want.
Also, the response to GET package, returns more data such as "date of creation", "created by', etc. How to handle this?
Thanks to anyone clarifying this.
To recap the linked Q&A - you can have a single model and define GET-only properties as
readOnly: true
.– Helen
Sep 8 '18 at 7:27
readOnly: true
Links are useful. I take this to be the answer I am looking for.
– Mopparthy Ravindranath
Sep 8 '18 at 16:38
1 Answer
1
Some example code in typescript i <3 typescript
There would be one class(model) which would take handle use Input, other class that would handle the output.
export class UserInput
username:string,
password:string,
email:String
export class UserData extends UserInput
id or _id : string
export class UserOutput
users : UserData
What this means is :
when you make a post request you use the userInput model
when you user the get request to get a user/list of users you use the userOutput model
fields/columns like "date of creation", "created by', etc. would automatically be handeled by your data model.
Dear Bro, I upvoted your answer to neutralize the downvote by someone. I don't like ppl downvoting without justifying why they do so. May be to exhibit their ego. You have attempted honestly to answer the question. My thumbs up to you.
– Mopparthy Ravindranath
Sep 8 '18 at 16:23
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Possible duplicate of Re-using model with different required properties, How to keep the single resource representation approach using OpenAPI spec
– Helen
Sep 8 '18 at 7:25