swagger-api / swagger-api/swagger-codegen

Generated Spring Controller lacks of @Validated annotation

Open
#8,727 0 comments 4 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

-The problem:
Query\PathParam arguments aren't validated by Spring.
-Solution:
Add @org.springframework.validation.annotation.Validated annotation along with @Controller
-Temporary hack:

    doLast {
        def generator = new io.swagger.codegen.DefaultGenerator().opts(swaggerConfig.toClientOptInput())
        def generatedFiles = generator.generate()
        println 'Applying HACKFIX: add @Validated on controllers'
        generatedFiles.findAll { it.name.contains('Controller') }
                .each { it.write(it.text.replaceFirst("@Controller", "@Controller\n@org.springframework.validation.annotation.Validated")) }
    }

-Note:
Please remember to add MethodValidationPostProcessor to Spring context

@Configuration
public class AppConfig {
    @Bean
    public MethodValidationPostProcessor methodValidationPostProcessor() {
        MethodValidationPostProcessor methodValidationPostProcessor = new MethodValidationPostProcessor();
        methodValidationPostProcessor.setProxyTargetClass(true);
        return methodValidationPostProcessor;
    }
}

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 tracing how DefaultGenerator produces Spring controller files and locate the template or generation path that emits @Controller. Confirm how the generated controllers handle Query and PathParam validation, then ensure @Validated is included alongside @Controller and verify the generated output. The issue also notes that consumers need MethodValidationPostProcessor in their Spring context.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.