Skip to content

Preventing Primitive boolean from Being Converted to Boxed Boolean in Java Client Generation #21350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mhashemi opened this issue May 29, 2025 · 2 comments

Comments

@mhashemi
Copy link

Hi,

I've generated Java clients using the restTemplate library from my openapi.yaml file. One of the schema properties is defined as a primitive boolean, but in the generated model, it has been converted to the boxed type Boolean. This behavior is not desired in my case.

Is there a way to configure the OpenAPI Generator CLI to preserve primitive types and prevent them from being overridden as boxed types?

Thank you in advance for your help.

Best regards.

@jpfinne
Copy link
Contributor

jpfinne commented May 30, 2025

@mhashemi you can use type-mappings to change Boolean into boolean

Using the cli (See https://openapi-generator.tech/docs/usage/)

--type-mappings=Boolean=boolean

you can perform the same in maven. See https://github.com/OpenAPITools/openapi-generator/tree/master/modules/openapi-generator-maven-plugin for maven

warning: the conversion applies even if the field is non required.

For full controll it might be possible to extend the JavaClientCodegen generator and override fromProperty().

In pseudo code:

public CodegenProperty fromProperty(String name, Schema p, boolean required, boolean schemaIsFromAdditionalProperties) {
   CodegenProperty  c = super.fromProperty(name, p, required, schemaIsFromAdditionalProperties);
   if (required && "Boolean".equals(c.datatypeWithEnum)) {
      c.datatypeWithEnum = "boolean";
   }
   return c;
}

This might be a nice new feature for all java primities (int, boolean...)

See https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java#L4122

And call a new getTypeDeclaration method

public String getTypeDeclaration(Schema schema, boolean required) {
      return getTypeDeclaration(schema);
}

In AbstractJavaClientCodegen.java override the method and optionally handle the conversion for required fields

@mhashemi
Copy link
Author

mhashemi commented Jun 4, 2025

Thanks for the response. Definitely perfect solution. My issue was the fact that in the source code, using primitive types i.e. boolean comes with default value, i.e. false. However after generating openapi, and from it, generate clients, the generated Java class will utilise Boxed types i.e. Boolean, with null value.
I address that with @Schema(defaultValue=“false”) on the source, so the generated Java class comes with default value i.e. false.
Thanks by the way.

@mhashemi mhashemi closed this as completed Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants