Skip to content

Missing Enum Values and validation code #54

@mayurpate

Description

@mayurpate

I am trying to generate Golang code for following openapi 3 spec. I have doubts regarding Go code generation for enum related types.

openapi: "3.0.0"
info:
  version: 1.0.0
  title: Swagger Petstore
  description: A sample API that uses a petstore as an example to demonstrate features in the OpenAPI 3.0 specification
  termsOfService: http://swagger.io/terms/
  contact:
    name: Swagger API Team
    email: apiteam@swagger.io
    url: http://swagger.io
  license:
    name: Apache 2.0
    url: https://www.apache.org/licenses/LICENSE-2.0.html
servers:
  - url: http://petstore.swagger.io/api
paths:
  /pets:
    post:
      summary: Post a PetType Information.
      description: Post a PetType Information.
      operationId: postPets
      requestBody:
        content:
          application/json:
            schema:
              oneOf:
                - $ref: '#/components/schemas/Cat'
                - $ref: '#/components/schemas/Dog'
      responses:
        '200':
          description: Updated
    get:
      summary: Returns a pet info by given petType.
      description: Get Pet Infrmation based on pet type.
      operationId: getPets
      parameters:
        - $ref: '#/components/parameters/PetType'
        - in: query
          name: petColor
          schema:
            type: string
      responses:
        '200':
          description: Get Pet Type 
          content:
            application/json: 
              schema:
                oneOf:
                  - $ref: '#/components/schemas/Cat'
                  - $ref: '#/components/schemas/Dog'
components:
  schemas:
    Pet:
      type: object
      properties:
        color:
          $ref: '#/components/schemas/Color'
        name:
          type: string
          nullable: true
        owners:
          type: array
          nullable: false
          items:
            type: object
            properties:
              ownerName:
                type: string
              ownerAddress:
                type: string
                nullable: true
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          required:
            - bark
          properties:
            bark:
              type: boolean
            breed:  
              type: string
              enum: [Dingo, Husky, Retriever, Shepherd]
              nullable: true
    Cat:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          properties:
            hunts:
              type: boolean
              nullable: true
            age:
              type: number
              format: float
              nullable: true
    Color:
      type: string
      nullable: true
      enum: 
        - black
        - white
        - red
        - green
        - blue
    PetTypeEnum:
      type: string
      enum:
        - cat
        - dog
  parameters:
    PetType:
      name: petType
      in: query
      required: true
      schema:
        $ref: '#/components/schemas/PetTypeEnum'

In the generate go code, enums defined in the YAML such as 'Color' and 'PetTypeEnum' are generated as String type which is expected.

type Color string 

But there are no 'const' generated for possible set of enum values defined in the input spec. I was exepcting following code to be generated with above code.

const (
        BLACK Color = "black"
        WHITE Color = "white"
        RED Color = "red"
        GREEN Color = "green"
        BLUE Color = "blue"
) 

Is this behaviour by design? I also do not see any validation function which will validate the input value against the possible enum values for a given field.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions