Is it possible to use an enum as path parameter in yaml?

Viewed 5473

There is a similar question asked in Swagger: Reusing an enum definition as query parameter . My question is if I can use an enum (re-usable or non-reusable). Whenever I try to do this I get errors however using string does not give any error

/path/{protocol}:
  patch:
    summary:
    operationId:
    tags:
    parameters:
      - name: protocol
        in: path
        description: # some description
        required: true
        schema:
          $ref: "#/components/schemas/ProtocolType"

   ProtocolType:
     type: string
     default: abcd
     enum:
      - abcd
      - aaa
      - bbb

My question is if the above example is valid or what possible changes I should try. I am using OpenAPI 3.0.0 .

ERRORS:

Compilation errors in XX.client.cpp
XX.client.cpp: In static member function ‘static void 
XX::SendSetProtocolReqRequest(std::string, const 
XX::model::SetProtocolReq_Request*, 
HTTPRequestEventContext::Ptr, uint64_t, HTTPClient*, FSM*, Statistics*, 
std::string, bool)’:
XX_Management.client.cpp:1822:33: error: no matching function for call to 
‘Json::ToValue(XX::model::XXEnumProtocolType*, 
framework::json::Value*)’
 Json::ToValue(&param, &value);  
                             ^

I don't have much knowledge about XX.client.cpp . It is an auto-generated file which is built after yaml file is compiled.

1 Answers

For SwaggerUI - using a 2.0 spec, you have to use inline enums such as:

swagger: '2.0'
info:
  title: Report API
  version: v1
paths:
  /report/{reportType}:
    get:
      tags:
      - ReportController
      parameters:
      - in: path
        name: reportType
        required: true
        type: string
        enum: [foo, bar, baz]

In 3.0, you can use a reference:

---
openapi: 3.0.1
info:
  title: Report API
  version: v1
paths:
  "/report/{reportType}":
    get:
      tags:
      - ReportController
      parameters:
      - name: exportType
        in: path
        description: ''
        required: true
        schema:
          "$ref": "#/components/schemas/ReportType"
components:
  schemas:
    ReportType:
      enum:
      - foo
      - bar
      - baz
      type: string
Related