Skip to content

@DefaultValue value is not cast to the correct type for request parameters #4798

@OllieKosh

Description

@OllieKosh

Problem

After upgrading swagger-core from 2.2.22 to 2.2.25, the @DefaulValue value is now always cast to String, even for parameters of type Integer, Boolean or other simple types. I am using OpenAPI 3.1.

Expected behavior

@DefaultValue#value is represented in the right format in the spec, so the non-string types are not wrapped in quotes.

For example for the following endpoint

@GET
@Path("/test")
public void getTest(@DefaultValue(value = "true") @QueryParam(value = "myBool") Boolean myBool) {}

the contract representing the parameter would look like this (in 2.2.22)

parameters:
- name: myBool
  in: query
  schema:
    type: boolean
    default: true

but instead the parameter default value is represented as String (wrapped in quotes) (in 2.2.25)

parameters:
- name: myBool
  in: query
  schema:
    type: boolean
    default: "true"

Reproducer

I am able to reproduce this by adding a test to ReaderTest class

    @Test(description = "@DefaultValue annotation value is cast to the right type")
    public void newTest(){
        SwaggerConfiguration config = new SwaggerConfiguration().openAPI31(true).openAPI(new OpenAPI());
        Reader reader = new Reader(config);
        OpenAPI openAPI = reader.read(ExampleResource.class);

        String yaml = "openapi: 3.1.0\n" +
                "paths:\n" +
                "  /test:\n" +
                "    get:\n" +
                "      operationId: getTest\n" +
                "      parameters:\n" +
                "      - name: myBool\n" +
                "        in: query\n" +
                "        schema:\n" +
                "          type: boolean\n" +
                "          default: true\n" +
                "      - name: myInt\n" +
                "        in: query\n" +
                "        schema:\n" +
                "          type: integer\n" +
                "          format: int32\n" +
                "          default: 1\n" +
                "      responses:\n" +
                "        default:\n" +
                "          description: default response\n" +
                "          content:\n" +
                "            '*/*': {}";

        SerializationMatchers.assertEqualsToYaml31(openAPI, yaml);
    }

with ExampleResource containing this endpoint

    @GET
    @Path("/test")
    public void getTest(
            @DefaultValue(value = "true") @QueryParam(value = "myBool") Boolean myBool,
            @DefaultValue(value = "1") @QueryParam(value = "myInt") Integer myInt) {
    }

Investigation

Similar to this issue the parameter schema was previously typed (like IntegerSchema, StringSchema etc) so the default value would be cast to the right type by calling the appropriate typed schema's cast() method. However, this upgrade and the fact that 3.1 version means to represent everything as a JsonSchema, the _default is not cast and always set to the String type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions