POST方法是否会在openapi 3.0.2中创建新实体并返回该实体的ID?

时间:2019-06-18 21:15:29

标签: api swagger openapi

我了解openapi 3.0.2中的POST方法应该创建一个新实体,并返回该实体的ID。当我通过ID向GET或DELETE添加该实体的其他路由时,出现404错误。我不太清楚为什么会这样。

这是我的帖子和获取方法:

/api/globalorderdays:
post:
  tags:
    - Setup Global Order Days
  summary: Allows user to add order days and holidays to multiple 
           sessions.
  requestBody:
    required: true
    description: put text here
    content:
      application/json:
        schema:
            $ref: '#/components/schemas/GlobalOrderSetupInfo'
  responses:
    201:
      description: Created
    400:
      description: Bad request
    401:
      description: Unauthorized

/api/globalorderdays/{Id}:
get:
  tags:
    - Setup Global Order Days
  summary: put text here
  parameters:
    - in: path
      name: Id
      required: true
      description: put text here
      schema:
        type: integer
        example:
  responses:
    200:
      description: Success
      content:
        application/json:
          schema:
              $ref: '#/components/schemas/GlobalOrderSetupInfo'
    400:
      description: Bad request
    401:
      description: Unauthorized

/api/globalorderdays/{Id}:
delete:
  tags:
    - Setup Global Order Days
  summary: Allows user to delete added order days
  parameters:
    - in: path
      name: Id
      required: true
      description: put text here
      schema:
        type: integer
        example:
  responses:
    204:
      description: Deleted
    400:
      description: Bad request
    401:
      description: Unauthorized

以下是组件:

GlobalOrderSetupInfo:
  description: 'Put Text Here'
  type: object
  properties:
    Id:
      type: integer
      nullable: true
    AvailableHolidayList:
      type: string
      nullable: true
    SelectedOrderHolidays:
      type: string
      nullable: true
    SelectedHolidays:
      type: string
      nullable: true
    OrderDays:
      type: string
      nullable: true
    NoOrderDays:
      type: string
      nullable: true
    AllSessionList:
      uniqueItems: false
      type: array
      items:
        $ref: '#/components/schemas/SessionInfoList'
    SessionIdString:
      type: string
      nullable: true

SessionInfoList:
  description: 'Put Text Here'
  type: object
  properties:
    Id:
      type: integer
      nullable: true
    SessionID:
      type: integer
      nullable: true
    Name:
      type: string
      nullable: true
    Type:
      type: string
    GroupName:
      type: string
    IsChecked:
      type: boolean
      default: false
    SetupID:
      type: string
      nullable: true

我希望能够通过ID检索/删除实体,但我会返回404错误

1 个答案:

答案 0 :(得分:0)

您的规格有几个问题。

  1. /api/globalorderdays/{Id}路径重复了几次。这是无效的。

    # Incorrect
    
    /api/globalorderdays/{Id}:
      get:
        ...
    
    /api/globalorderdays/{Id}:
      delete:
        ...
    

    相反,只需指定一次路径,并在其下方列出其所有HTTP方法,如下所示:

    /api/globalorderdays/{Id}:
      get:
        ...
      delete:
        ...
    
  2. 参数示例缺少该值:

    schema:
      type: integer
      example:   # <-----
    

    YAML中的缺失值等效于null,但是null对于整数模式不是有效的示例。添加适当的整数示例,例如example: 1,或从这些模式中删除example关键字。

一旦解决了这些问题,GET和DELETE的模拟就可以正常工作。