OpenAPITools / OpenAPITools/openapi-generator

[BUG] Default value is not recognized at controller end while using spring openapigenerator

Open
#16,944 0 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

below is my openapi spec

get:
  summary: Get User By Id
  operationId: getUserById
  parameters:
    - in: query
      name: id
      description: Unique Id of an User
      schema:
        type: integer
        default: 10

I am using spring openapigenerator to generate interface API code which will be implemented in controller using below configurations in build.gradle,

def swaggerSourceFile = "$projectDir/src/main/resources/api.yml"
def swaggerTargetFolder = "$projectDir/src/generated/java"
def swaggerConfigFile = "$projectDir/swaggerConfig.json"

openApiGenerate {
    generatorName = "spring"
    configFile = "${swaggerConfigFile}".toString()
    inputSpec = "$swaggerSourceFile".toString()
    outputDir = "$swaggerTargetFolder".toString()
    globalProperties = [modelDocs: "false", apiTests: "false", apiDocs: "false"]
    generateApiTests = false
}

and my swaggerconfigFile has below configs,

{
    "interfaceOnly": true,
    "library": "spring-boot",
    "useDefaultValue": true,
    "dateLibrary" : "java8",
    "typeMappings" : {
	    "OffsetDateTime":"Instant"
    },
    "importMappings" : {
	    "java.time.OffsetDateTime":"java.time.Instant"
    },
    "useGzipFeature" : true,
    "useRuntimeException" : true,
    "apiPackage" : "com.api",
    "modelPackage"  : "com.api.model",
    "modelNameSuffix" : "DTO",
    "sourceFolder": "",
    "useBeanValidation" : false,
    "performBeanValidation": false
}

Here default value for the input is configured as 10 and its also available in generated API interface given below, but while accessing the API, controller don't recognize the default value and it got null if no data provided for input.

Generated Interface API code:

 @RequestMapping(
        method = RequestMethod.GET,
        value = "/user",
        produces = { "application/json" }
    )
    default ResponseEntity<UserDTO> getUserById(
        @Parameter(name = "id", description = "Unique Id of an User", in = ParameterIn.QUERY) 
        @RequestParam(value = "id", required = false, defaultValue = "10") Integer id
    ) {
     }

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 OpenAPI parameter in api.yml, the spring generator configuration in build.gradle and swaggerConfig.json, and the generated API interface shown in the report. Reproduce the generated controller behavior when id is omitted, then trace the spring generator's handling of defaultValue. Done means the generated endpoint consistently exposes and applies the declared default value, with a regression test covering the case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.