swagger-api / swagger-api/swagger-codegen

Codegen Ignoring @Schema(nullable=false) annotation (JAVA) with redundant null checks.

Open
#10,024 0 comments 0 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

The code generated via swagger-codegen is placing a nullcheck on a non-nullable field. As this field is also required, a redundant nullcheck is being performed. 1 nullcheck for the param not being null to avoid throwing exception and another nullcheck before placing the headers in the localVarHeaderParams.

If the parameter's schema annotation is defined as "nullable=false", there shouldn't be a nullcheck on this value.

Swagger-codegen version

3.0.8

Command line used for generation

(use xml for ant build, extracted cmd line args)
generate -i https://serverhost22/api/openapi.json -c config.json -l java --library jersey2

Steps to reproduce
  1. Create a String argument for a given operation.

  2. Annotate as follows: See Parameter(Schema())

@Operation(summary = "Release information", tags = {SwaggerConstants.TAG_CRUISE}, description = "Executes to release",
parameters = {@Parameter(in = ParameterIn.HEADER, name = SwaggerConstants.SESSION_ID, schema = @Schema(nullable = false, required = true))},
responses = {@ApiResponse(responseCode = "200", description = "ReleaseStateroomResponse with stateroom release information.",
content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(implementation = Release.class)))}) }

  1. When the code is generated, the method will look like this with a redunant nullcheck:
    image
Suggest a fix/enhancement

If the param has the Schema annotation with nullable = false, don't insert a nullcheck.

I'd be happy to help but am not sure where to look in the codegen files to get a fix implemented. Let me know if this is my problem and is by design.

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 issue with swagger-codegen 3.0.8 using the provided OpenAPI annotation and the Java jersey2 generation command. Trace the generated operation method's parameter handling and verify that a parameter marked nullable=false and required=true no longer receives the redundant null check, while other parameters retain their existing behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
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.