swagger-api / swagger-api/swagger-codegen

[Spring] Support @Email constraint

Open
#10,202 3 comments 8 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

Add support for @javax.validation.constraints.Email

IF useBeanValidation and performBeanValidation are true
AND format: email specified for path param
THEN should be @Email on parameter

Swagger-codegen-maven-plugin version

3.0.18

pom.xml
<plugins>
  <plugin>
    <groupId>io.swagger.codegen.v3</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <executions>
      <execution>
        ...
        <configuration>
          ...
          <language>spring</language>
          <configOptions>
            <useBeanValidation>true</useBeanValidation>
            <performBeanValidation>true</performBeanValidation>
            ...
          </configOptions>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>
Swagger declaration file content
paths:
  /users/{email}:
    get:
      operationId: getUserByEmail
      parameters:
        - in: path
          name: email
          schema:
            type: string
            format: email
Expected behavior

From yaml above should be method containing @Email constraint generated:

import javax.validation.constraints.Email;
...
@RequestMapping(value = "/users/{email}", produces = { "application/json" }, method = RequestMethod.GET)
ResponseEntity<User> getUserByEmail(@Email @PathVariable("email") String email);

Currently @Email annotation is missing.

Suggest a fix/enhancement
  • Add to CodegenParameter.class & CodegenProperty.class new variable boolean isEmail and implement all necessary logic.

  • Add to beanValidationCore.mustache next line:

{{#useBeanValidation}}{{#performBeanValidation}}{{#isEmail}}@Email{{/isEmail}}{{/performBeanValidation}}{{/useBeanValidation}}

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 CodegenParameter.class and CodegenProperty.class represent schema formats, then inspect beanValidationCore.mustache and the Spring generator's existing validation handling. Add email-format support so the template emits @Email only when useBeanValidation and performBeanValidation are enabled, and verify the generated path-parameter signature matches the issue's example.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.