0

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.

Mopparthy Ravindranath
  • 3,014
  • 6
  • 41
  • 78
  • 1
    Possible duplicate of [Re-using model with different required properties](https://stackoverflow.com/questions/40839706/re-using-model-with-different-required-properties), [How to keep the single resource representation approach using OpenAPI spec](https://stackoverflow.com/a/41152486/113116) – Helen Sep 08 '18 at 07:25
  • 1
    To recap the linked Q&A - you can have a single model and define GET-only properties as `readOnly: true`. – Helen Sep 08 '18 at 07:27
  • Links are useful. I take this to be the answer I am looking for. – Mopparthy Ravindranath Sep 08 '18 at 16:38

1 Answers1

0

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.

mobman
  • 89
  • 1
  • 8