OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] @Size not generated in the method parameters which are treated as headers

Open
#7,125 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
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)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

I'm trying to generate some javax.validation annotations in my generated Spring API files. In the parameters section I use reference to the object. When I try to add a size validation on a parameter by adding minItems or maxItems for example, the generated method does not contain the expected javax.validation @Size annotation

Actual:

@ApiOperation(value = "someValue", nickname = "someNickName", notes = "someNotes", tags={ "someTag", })
    @ApiResponses(value = { 
        @ApiResponse(code = 202, message = "success") })
    @RequestMapping(value = "/someUrl/v1/smth",
        consumes = { "application/json" },
        method = RequestMethod.POST)
    default ResponseEntity<Void> someMethod(@ApiParam(value = "someValueHeaderOne" ,required=true) @RequestHeader(value="HeaderOne", required=true) String headerOne,@ApiParam(value = "someValueHeaderTwo" ,required=true) @RequestHeader(value="HeaderTwo", required=true) UUID headerTwo, @ApiParam(value = "Request Body" ,required=true )  @Valid @RequestBody SomeBody someBody) {
        return getDelegate().someMethod(headerOne, headerTwo, someBody);

Expected:

@ApiOperation(value = "someValue", nickname = "someNickName", notes = "someNotes", tags={ "someTag", })
    @ApiResponses(value = { 
        @ApiResponse(code = 202, message = "success") })
    @RequestMapping(value = "/someUrl/v1/smth",
        consumes = { "application/json" },
        method = RequestMethod.POST)
    default ResponseEntity<Void> someMethod(@ApiParam(value = "someValueHeaderOne" ,required=true) @RequestHeader(value="HeaderOne", required=true) @Size (min=1, max=10) String headerOne,@ApiParam(value = "someValueHeaderTwo" ,required=true) @RequestHeader(value="HeaderTwo", required=true) @Size (min=1, max=10) String headerTwo, @ApiParam(value = "Request Body" ,required=true )  @Valid @RequestBody SomeBody someBody) {
        return getDelegate().someMethod(headerOne, headerTwo, someBody);

When I try this by manually modifying the generated interface, the validation works great and throw errors when I send an empty header

openapi-generator version

4.1.3 (maven plugin). I also tried the 4.3.1 version and the latest version, the result is the same

OpenAPI declaration file content or url
{
  "openapi": "3.0.1",
  "info": {
    "title": "title",
    "description": "description",
    "version": "1.0.0",
    "contact": {
      "name": "name",
      "url": "url",
      "email": "email.com"
    }
  },
  "paths": {
    "/someUrl/v1/smth": {
      "post": {
        "tags": [
          "someMethod"
        ],
        "summary": "summary",
        "description": "description",
        "operationId": "sm",
        "parameters": [
          {
            "$ref": "#/components/parameters/HeaderOne"
          },
          {
            "$ref": "#/components/parameters/HeaderTwo"
          }
        ],
        "requestBody": {
          "description": "Request Body",
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SomeBody"
              }
            }
          },
          "required": true
        },
        "responses": {
          "202": {
            "description": "success"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "SomeBody": {
        "type": "object",
        "required": [
          "id"
        ],
        "properties": {
          "id": {
            "type": "string",
            "example": "123",
            "description": "description"
          }
        }
      }
    },
    "parameters": {
      "HeaderOne": {
        "name": "HeaderOne",
        "in": "header",
        "description": "description",
        "required": true,
        "schema": {
          "type": "string",
          "minLength": 1,
          "maxLength": 10
        }
      },
      "HeaderTwo": {
        "name": "HeaderTwo",
        "in": "header",
        "description": "description",
        "required": true,
        "schema": {
          "type": "string",
          "minLength": 1,
          "maxLength": 10
        }
      }
    }
  }
}
Generation Details
                 <execution>
                        <id>generate-sources-client</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/conf/swagger/smth.json</inputSpec>
                            <generatorName>spring</generatorName>
                            <generateApiTests>false</generateApiTests>
                            <generateApiDocumentation>false</generateApiDocumentation>
                            <generateModelTests>false</generateModelTests>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <apiPackage>${my.package}.api.smth</apiPackage>
                            <modelPackage>${my.package}.api.smth.model</modelPackage>
                            <library>spring-boot</library>
                            <configOptions>
                                <java8>true</java8>
                                <library>resttemplate</library>
                                <library>spring-boot</library>
                                <delegatePattern>true</delegatePattern>
                                <dateLibrary>java8-offsetdatetime</dateLibrary>
                                <performBeanValidation>true</performBeanValidation>
                                <useBeanValidation>true</useBeanValidation>
                                <useTags>true</useTags>
                            </configOptions>
                            <environmentVariables>
                                <supportingFiles>ApiUtil.java</supportingFiles>
                            </environmentVariables>
                        </configuration>
                    </execution>
Related issues/PRs

This could be related to this issue - https://github.com/OpenAPITools/openapi-generator/issues/4122

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 the provided OpenAPI 3.0.1 document using the Spring generator and the performBeanValidation and useBeanValidation settings. Compare generated API method parameters for the referenced headers with the expected @Size output; done when minLength and maxLength produce the required annotations on both headers and the behavior is covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.