Skip to content

Enum/oneOf of object type in request body property #779

Open
@mheh

Description

@mheh

Question

Trying to achieve

I'm having an issue trying to understand correct syntax or if it's possible to use the oneOf declaration within a property of a requestBody. My main goal would be in this codebase having a POST request that accepts input on a property 'cost' of oneOf the available objects. The resulting code would allow me to do something like this in a Vapor server

guard case .json(let body) = input.body else { throw Abort(.badRequest) }
switch body.cost {
case .productCostId(let costRequest):
  // guard let foundCost = try await ProductCost.find(costRequest.id, on: req.db) else {
  // throw Abort(.notFound)
  // }
case .manualCostAmount(let manualCostAmount):
  // let myCost = try make decimal from string
}

What have I tried

I'm looking at this code right now for the entirety of the request, however I've gone through iterations of moving the type: object to the enum cases, to the property itself. Using schema: or oneOf: or enum: in various fashion. I also tried comparing what I was doing to the parameters usage of the request. The section I'm having difficulty with is the final property: cost

post:
  operationId: inventoryProductItemCreate
  summary: Create a new inventory product item
  tags:
    - Inventory
  requestBody:
    description: Inventory product item object to be created
    required: true
    content:
      application/json:
        schema:
          required:
            - productId
            - boxId
            - cost
          properties:
            productId:
              type: integer
              description: User defined name of the inventory location
            boxId:
              type: integer
              description: The ID of the box where the product item is created at
            cost:
              description: The cost of the product item
              enum:
                - name: productCostId
                  schema:
                    type: object
                    required:
                      - productCostId
                    properties:
                      productCostId:
                        type: integer
                        description: The ID of a product cost to use for the product item. If specified the manual cost amount is ignored.
                - name: manualCostAmount
                  schema:
                    type: object
                    required:
                      - manualCostAmount:
                    properties:
                      manualCostAmount:
                        type: string
                        description: A manual cost amount to enter for the product item

What didn't work as expected

I believe this is just a syntax issue but felt like it was a bit obscure in comparison to the enum types where single instances look like:

type: string
- UK
- US

The result is an empty ValueContainer or Object that I can't use.

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/supportAdopter support requests.status/triageCollecting information required to triage the issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions