OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Spring] Enum in path variable or query param is passed as String instead of Enum

Open
#17,814 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • [ X ] Have you provided a full/minimal spec to reproduce the issue?
  • [ X ] Have you validated the input using an OpenAPI validator (example)?
  • [ X ] Have you tested with the latest master to confirm the issue still exists?
  • [ X ] Have you searched for related issues/PRs?
  • [ X ] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When defining a path parameter or a query parameter as an enum, the a string is passed to the controller method. If defined as a component schema, an enum is correctly created and passed to the controller.

In the following example, a PetsEnum is created and passed to the controller for the parameters queryEnumAsSchema and pathEnumAsSchema. For the other two enum parameters, a String is passed. An enum should be passed for all the parameters

openapi-generator version

7.2.0

OpenAPI declaration file content or url
openapi: "3.0.3"
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: http://petstore.swagger.io/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: pathEnum
          in: path
          required: true
          schema:
            type: string
            enum:
              - VAL1
              - VAL2
        - name: queryEnum
          in: query
          required: true
          schema:
            type: string
            enum:
              - VAL1
              - VAL2
        - name: pathEnumAsSchema
          in: path
          required: true
          schema:
            $ref: "#/components/schemas/PetsEnum"
        - name: queryEnumAsSchema
          in: query
          required: true
          schema:
            $ref: "#/components/schemas/PetsEnum"
      responses:
        '200':
          description: A paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
components:
  schemas:
    PetsEnum:
      type: string
      enum:
        - Val1
        - Val2
Generation Details

Run it with the gradle plugin:

plugins {
    id "org.openapi.generator" version "7.2.0"
}

openApiGenerate {
    generatorName = "spring"
    inputSpec = "${projectDir}/src/main/resources/openapi/app-spec.yaml"
    outputDir = "${buildDir}/generated"
    apiPackage = "newapp.rest.api"
    invokerPackage = "newapp.rest.handler"
    modelPackage = "newapp.rest.model"
    skipOperationExample = true
    configOptions = [
            useSpringBoot3: "true",
            delegatePattern: "true"
    ]
    globalProperties =[
            apis: '',
            models: '',
    ]
}
Steps to reproduce

Execute the openApiGenerate task

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Gradle openApiGenerate task using generatorName "spring" and the supplied OpenAPI YAML, then inspect the generated controller signatures for the four enum parameters. Done means inline path and query enums are passed as enum values, matching the behavior already shown for the component-schema enum; no specific test file is named in the report.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.