OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA][jaxrs-cxf-cdi] Generating invalid java code (@ApiParam annotation) for defaultvalue of type integer in parameters

Open
#8,535 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

OpenApi spec authorizes parameters of several types:
Parameters (headers)
Allowed data types
This includes string, boolean, integer...

In the jaxrs-cxf-cdi implementation, it generates an @ApiParam annotation from the io.swagger.annotations package:
ApiParam annotation reference
The type of defaultValue is String (java.lang)

Generation of boolean or string parameter outputs a String (java) for defaultValue (with quotes)
For integers, the @ApiParam annotation has an integer in the generated defaultValue (without quotes) (should be a String, with quotes)

Therefore, there is a compilation error with the generated code.

openapi-generator version

This bug exists in 5.0.0 and today (25 January 2021) compiled source code.
Default value (string) was broken in 4.x.x (see related issues)

OpenAPI declaration file content or url

Here is an extract of the definition that fails to generate valid jaxrs-cxf-cdi java code:

{
  "openapi" : "3.0.1",
  "paths" : { "/abug" : { "get" : {
  "parameters": 
{
  "name" : "bughere",
  "in" : "header",
  "description" : "Valid openapi definition but not jaxrs-cxf-cdi (ApiParam annotation)",
  "schema" : {
    "type" : "integer",
    "format" : "int32",
    "default" : 200
} } } } } }

Full json here

Generation Details

Default config for jaxrs-cxf-cdi generator (see steps to reproduce)
Output is :

package org.openapitools.api;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import io.swagger.annotations.*;
import javax.validation.constraints.*;
@Path("/abug")
@RequestScoped
@Api(description = "the abug API")
public class AbugApi  {
  @Context SecurityContext securityContext;
  @Inject AbugApiService delegate;
    @GET
    @Produces({ "application/xml", "application/json", "text/plain" })
    @ApiOperation(value = "bug test", notes = "desc", response = Void.class, tags={ "bug" })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "default response", response = Void.class) })
    public Response id(@ApiParam(value = "type string test", defaultValue="stringTest") @DefaultValue("stringTest")  @QueryParam("something") String something, @ApiParam(value = "type boolean test", defaultValue="true") @DefaultValue("true")  @QueryParam("is_something") Boolean isSomething,  @ApiParam(value = "Valid openapi definition but not jaxrs-cxf-cdi (ApiParam annotation)" , defaultValue=200)@HeaderParam("bughere") Integer bughere) {
        return delegate.id(something, isSomething, bughere, securityContext);
    }
}
Steps to reproduce
  1. java -jar openapi-generator-cli.jar generate -g jaxrs-cxf-cdi -i bug.json -o bug
  2. compile java code (using eclipse IDE here)
Related issues/PRs

Double quotes were appearing prior to 5.0.0:
[BUG] [jaxrs-cxf-cdi] Compilation issue in generated code when there is default value set for header parameters

Similar to #7871 but for Spring:
Default values for string-type query parameters are generated with two sets of quotes

Suggest a fix

Might be related to the mustache templates located:
modules/openapi-generator/src/main/resources/JavaJaxRS/cxf-cdi

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 jaxrs-cxf-cdi templates under modules/openapi-generator/src/main/resources/JavaJaxRS/cxf-cdi and reproduce the issue with the supplied OpenAPI snippet and generation command. Verify that an integer header default produces a quoted @ApiParam defaultValue and that the generated Java compiles.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.