swagger-api / swagger-api/swagger-codegen

OpenApi boolean type variable java generated code is not correct.

Open
#12,046 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I am using openapi: 3.0.1 specification, defined an attribute with type: boolean but generated java code has boxed type of Boolean instead of boolean. Due to this, api parameter accepting null values.

I tried setting nullable:false, but no luck.

When I sent a request with hasBalance: null the same coming to server side without validating null checks.

Swagger-codegen version

// https://mvnrepository.com/artifact/io.swagger.codegen.v3/swagger-codegen-cli/3.0.36
swaggerCodegen 'io.swagger.codegen.v3:swagger-codegen-cli:3.0.36'

Swagger declaration file content or url
Command line used for generation

I used build.gradle file in springboot applicaiton.


buildscript {
	repositories {
	}

	dependencies {
	}
}

plugins {

        id 'java'
	id 'org.springframework.boot' version '3.0.1'
	id 'io.spring.dependency-management' version '1.1.0'	id 'org.hidetake.swagger.generator' version '2.19.2'

}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
   mavenCentral()
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.boot:spring-boot-starter-validation'
	
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	
	// For building the code generated by swagger codegen.
	// https://mvnrepository.com/artifact/io.swagger.core.v3/swagger-annotations/2.2.7
	implementation 'io.swagger.core.v3:swagger-annotations:2.2.8'

	// For generating code using swagger codegen.
	// https://mvnrepository.com/artifact/io.swagger.codegen.v3/swagger-codegen-cli/3.0.36
	swaggerCodegen 'io.swagger.codegen.v3:swagger-codegen-cli:3.0.36'


}

// https://openapi-generator.tech/docs/generators/java/
swaggerSources {
	dms {
		inputFile = file('specs/api/dms-openapi.yaml')
		code {
			language = 'spring'
			components = [models: true, apis: true]
			configFile = file('openapi-config.json')
			jvmArgs = ['--add-opens=java.base/java.util=ALL-UNNAMED']
		}
	}
}


Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement

If type: boolean should not accept null values until unless we specify nullable: true.

That mean in the generated java code variable type should be

  1. if type: boolean then it should be boolean aka primitive type.
  2. if type: boolean and property of nullable: true then it should be boxed type Boolean

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

Reproduce the generated model from specs/api/dms-openapi.yaml using the build.gradle setup and openapi-config.json. Then trace boolean and nullable handling in the Java Spring generator templates and compare the generated output with the reported BalanceRequest example. Done means the generator has a verified behavior for non-nullable and nullable boolean properties, backed by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.