Skip to content

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

Open
@timothee-peron

Description

@timothee-peron

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions