2

I am having trouble figuring out a way to create a DELETE method for the POST method I just created in my API design. The post takes in a requestBody of the GlobalOrderSetupInfo, within that object there is another object that will be an array of different sessions that I want to add the GlobalOrderSetupInfo info to, in the delete method I need that same info deleted but you cannot have a delete method with a requestBody. How do I go about creating it?

Here is my post method:

'/api/globalorderdays':
post:
  tags:
    - Setup Global Order Days
  summary: Allows user to add orderdays to multiple sessions
  requestBody:
    required: true
    description: put text here
    content:
      application/json:
        schema:
          type: object
          items:
            $ref: '#/components/schemas/GlobalOrderSetupInfo'
  responses:
    '201':
      description: Created
    '400':
      description: Bad request
    '401':
      description: Unauthorized
components:
schemas:
GlobalOrderSetupInfo:
  description: 'Put Text Here'
  type: object
  properties:
    Id:
      type: integer
    AvailableHolidayList:
      type: string
    SelectedOrderHolidays:
      type: string
      example: "New Year's Day, President's Day, Memorial Day, Labor Day, Veterans Day, Thanksgiving Day, Chistmas Day"
    SelectedHolidays:
      type: string
      example: "New Year's Day, President's Day, Memorial Day, Labor Day, Veterans Day, Thanksgiving Day, Chistmas Day"
    OrderDays:
      type: string
      example: "01/01/2000"
    NoOrderDays:
      type: string
      example: "01/01/2000"
    AllSessionList:
      uniqueItems: false
      type: array
      items:
        $ref: '#/components/schemas/SessionInfoList'
    SessionIdString:
      type: string
      example: "15"

SessionInfoList:
  description: 'Put Text Here'
  type: object
  properties:
    Id:
      type: integer
    SessionID:
      type: integer
    Name:
      type: string
      example: "Harbor"
    Type:
      type: string
    GroupName:
      type: string
      example: "PHACTS"
    IsChecked:
      type: boolean
      default: false
      example: true/false
    SetupID:
      type: string

1 Answers1

4

Typically your POST method creates a new entity, and returns the id of that entity. Then you might have additional routes to GET that entity by ID, update (PATCH) it, or DELETE it.

So in your example, the entry for DELETE might look like:

'/api/globalorderdays/{id}':
  parameters:
    - in: path
      name: id
      required: true
      schema:
        type: integer
  get:
    summary: Get orderdays by id
    responses:
      '200':
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/GlobalOrderSetupInfo'
  delete:
    summary: Delete orderdays by id
    responses:
      '204':
        description: Deleted
      '404':
        description: id not found
      '401':
        description: Unauthorized
lordbrain
  • 191
  • 3
  • I went ahead and followed what you said, now I do get a 204 when doing the delete method. To test that it actually deleted I created a get method to see if the {id} was created and when i execute the get method I get a 404 error. Does it have something to do with how I create my components? –  Jun 17 '19 at 22:46
  • I added a get route above. Does that match what you added? The GET takes an {id} parameter, just like DELETE. Also, openapi is just a spec - are you using the Auto Mocking feature of swagger hub or some other tool to generate the responses? – lordbrain Jun 19 '19 at 03:19
  • Yeah thats how I have it formatted. And yeah I've set up the Auto Mocking on swaggerhub as well as I have set up a mock server on postman and still get the same results. –  Jun 19 '19 at 16:35
  • I've never used swagger hub auto mocking, but from the docs it appears it will return the first response in the list. So if your 'get' entry lists '404' as the first item in the responses list, that's what it will return on every get request. – lordbrain Jun 19 '19 at 20:21