swagger-api / swagger-api/swagger-codegen

[Spring] Generate javax.validation annotations for @RequestHeader method arguments

Open
#7,937 2 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

Swagger specification allows us to enforce validation constraints on request headers such as maxLength, minLength, pattern etc. However, when we generate server code using Spring as a language, the validation annotations are missing from the generated code. This is not the case for request parameters though where validation constraints are indeed generated.

Swagger-codegen version

I use 2.2.2 but have verified that I can still reproduce it in 2.3.1

Swagger declaration file content or url
"customHeader": {
    "name": "X-Custom-Header",
    "in": "header",
    "required": true,
    "type": "string",
    "minLength": 3
}
Command line used for generation

I use a Gradle plugin to generate code and here is the configuration:

swagger {
    inputSpec = file('swagger.json').absolutePath
    outputDir = "${buildDir}/generated-sources"
    lang = 'spring'

    addDynamicProperty 'sourceFolder', 'src/swagger/java'

    additionalProperties = [
        basePackage: 'com.example.foo',
        invokerPackage: 'com.example.foo',
        modelPackage: 'com.example.foo.api.model',
        apiPackage: 'com.example.foo.api',
        configPackage: 'com.example.foo.config',
        interfaceOnly: true,
        java8: true
    ]

    systemProperties = [
        apis: '',
        models: ''
    ]
}
Steps to reproduce

Just generate code and look at the generated code. For the above header, I expect a @Size(min = 1) constraint to be added to the method signature but it's clearly missing.

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 by generating the Spring server code from the provided Swagger declaration and compare the @RequestHeader method signature with the request-parameter output. Trace the Spring generator templates or entry point responsible for parameter annotations; done means header constraints such as minLength, maxLength, and pattern appear as javax.validation annotations in the generated signature.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.