Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Not adding the correct javax.validation imports with a specific configuration #15849

Closed
5 of 6 tasks
CatKinKitKat opened this issue Jun 15, 2023 · 3 comments · Fixed by #15921
Closed
5 of 6 tasks

Comments

@CatKinKitKat
Copy link

CatKinKitKat commented Jun 15, 2023

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output? (If it was the same I wouldn't be here)
  • [Optional] Sponsorship to speed up the bug fix or feature request (example) (Je suis very poor, señorita. Maybe one day.)
Description

OpenAPI Java (Spring) generator is not adding the correct javax.validation imports with this specific configuration (Reactor, Spring, Optional-wrapped, etc check bellow). I believe is a bug with this specific configuration, because it usually works fine with some slightly similar configurations.

openapi-generator version

Spring Java Generator
Version 6.6.0

File

Yaml:

...
components:
  parameters:
    PageParameter:
      name: page
      in: query
      description: Page number
      required: false
      schema:
        type: integer
        format: uint32
        minimum: 1
        default: 1
    SizeParameter:
      name: size
      in: query
      description: Page size
      required: false
      schema:
        type: integer
        format: uint32
        minimum: 1
        default: 10
    SortParameter:
      name: sort
      in: query
      description: Sort
      required: false
      schema:
        type: string
        enum: [ id, name, surname, birthDate, phoneNumber, email ]
        default: id
    SortDirectionParameter:
      name: direction
      in: query
      description: Direction
      required: false
      schema:
        type: string
        enum: [ asc, desc ]
        default: asc

Pom XML:

...
            <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>${openapi-generator.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${project.basedir}/src/main/resources/static/api.yaml
                            </inputSpec>
                            <generatorName>spring</generatorName>
                            <apiPackage>eu.catkinkitkat.presentation.generated.api</apiPackage>
                            <modelPackage>eu.catkinkitkat.presentation.generated.model</modelPackage>
                            <configOptions>
                                <reactive>true</reactive>
                                <useLombok>true</useLombok>
                                <library>spring-boot</library>
                                <useOptional>true</useOptional>
                                <dateLibrary>java8</dateLibrary>
                                <useJakartaEe>false</useJakartaEe> <!-- use javax.* annotations instead of jakarta.* -->
                                <delegatePattern>true</delegatePattern>
                                <useBeanValidation>true</useBeanValidation>
                                <annotationLibrary>swagger2</annotationLibrary>
                                <documentationProvider>springdoc</documentationProvider>
                                <disallowAdditionalPropertiesIfNotPresent>false
                                </disallowAdditionalPropertiesIfNotPresent>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
...

Generated:

...
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Generated;
...
    default Mono<ResponseEntity<PersonSearchResponse>> searchPeople(Optional<String> name,
        Optional<String> surname,
        Optional<Integer> age,
        Optional<String> phoneNumber,
        Optional<String> email,
        Optional<String> address,
        Optional<@Min(1) Integer> page,
        Optional<@Min(1) Integer> size,
        Optional<String> sort,
        Optional<String> direction,
        ServerWebExchange exchange) {
        Mono<Void> result = Mono.empty();
        exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED);
        for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) {
            if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                String exampleString = "{ \"persons\" : [ { \"phoneNumber\" : \"phoneNumber\", \"address\" : \"address\", \"surname\" : \"surname\", \"name\" : \"name\", \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\", \"birthDate\" : \"2000-01-23\", \"email\" : \"email\" }, { \"phoneNumber\" : \"phoneNumber\", \"address\" : \"address\", \"surname\" : \"surname\", \"name\" : \"name\", \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\", \"birthDate\" : \"2000-01-23\", \"email\" : \"email\" } ], \"pagination\" : { \"total\" : 1, \"size\" : 6, \"page\" : 0 } }";
                result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
                break;
            }
        }
        return result.then(Mono.empty());

    }
...
Generation Details

Everything expected generates EXCEPT the import javax.validation.constraints.Min; line, breaking the maven compile goal because of unimported @Min annotation.

Steps to reproduce

Use mvn -U -T 1C clean install or IntelliJ's GUI IntelliJ's GUI with similar file contents.

@CatKinKitKat CatKinKitKat changed the title [BUG] Description [BUG] Not adding the correct javax.validation imports with a specific configuration Jun 15, 2023
@borsch
Copy link
Member

borsch commented Jun 15, 2023

Can you try to explicitly set <useBeanValidation>true</useBeanValidation> under configOptions?

@CatKinKitKat
Copy link
Author

CatKinKitKat commented Jun 19, 2023

Just tried. The behaviour is still happening. Also added it to the bug report.

@vad-systems
Copy link
Contributor

vad-systems commented Jun 25, 2023

I also stumbled upon this during some prototyping, and it seems to be caused primarily by delegatePattern=true.

It seems to me, the problem lies with the lack of the {{javaxPackage}}.validation.constraints.* import in the delegate template - an import for {{javaxPackage}}.validation.Valid was added with #15330 to resolve a similar issue, but the accompanying import for the constraints annotations is still absent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants