繁体   English   中英

用于 Java 的 OpenAPI Maven 插件生成不一致的类

[英]OpenAPI Maven Plugin for Java Generating Inconsistent Classes

我们正在使用 openapi-generator-maven-plugin 版本 5.0.1 来生成我们的 API。 我正在尝试指定一个包含 DTO 和文件的请求。

第一个奇怪的地方是生成的代码没有使用 DTO,它基本上将字段展平,因此 API 期望每个字段都被指定。 但是,我们并不太关心这个问题,因为我们可以只指定每个字段(尽管如果它按预期工作会很好)。

困扰我们的问题是为 API 和 API 代表生成的类彼此不一致。 生成的 API 将每个字段视为String 但是,API 代表将它们视为Optional<String> 因此,当我们尝试编译代码时,API 会出现编译错误,因为它正在将 String 传递给 Delegate,而 Delegate 需要Optional<String>

这是我们的 POM,以及相关的依赖项和插件配置:

        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.6.2</version>
        </dependency>
        <dependency>
            <groupId>org.openapitools</groupId>
            <artifactId>jackson-databind-nullable</artifactId>
            <version>0.2.1</version>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
...
<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.0.1</version>
    <executions>
        <execution>
            <id>processor-Generate</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>
                    ${project.basedir}/apis/innovation-ivp-inventory-accuracy-acl-api.yml
                </inputSpec>
                <generatorName>spring</generatorName>
                <apiPackage>${project.groupId}.inventory.accuracy.acl.api</apiPackage>
                <modelPackage>${project.groupId}.inventory.accuracy.acl.dto</modelPackage>
                <invokerPackage>${project.groupId}.inventory.accuracy.acl.api.handler</invokerPackage>
                <supportingFilesToGenerate>ApiUtil.java,OpenAPIDocumentationConfig.java
                </supportingFilesToGenerate>
                <configOptions>
                    <useTags>true</useTags>
                    <dateLibrary>java8-localdatetime</dateLibrary>
                    <java8>true</java8>
                    <delegatePattern>true</delegatePattern>
                    <useBeanValidation>true</useBeanValidation>
                    <useOptional>true</useOptional>
                    <configPackage>${project.groupId}.inventory.accuracy.acl.api</configPackage>
                </configOptions>
                <output>${project.build.directory}/generated-sources</output>
            </configuration>
        </execution>
    </executions>
</plugin>

这是我们的 OpenAPI 规范:

'/email':
  post:
    tags:
      - email-service
    summary: Email Service
    operationId: sendEmail
    requestBody:
      required: true
      content:
        multipart/mixed:
          schema:
            allOf:
              - $ref: '#/components/schemas/EmailRequestDTO'
              - type: object
                properties:
                  file:
                    type: string
                    format: binary
    responses:
      "200":
        description: OK
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/EmailResponseDTO'
components:
  schemas:
    EmailRequestDTO:
      type: object
      properties:
        sendTo:
          type: string
        sentFrom:
          type: string
        subject:
          type: string
        content:
          type: string
    EmailResponseDTO:
      type: object
      properties:
        status:
          type: string
        errorMessage:
          type: string

这里是OpenAPI生成的API class(注意参数都是String):

public interface EmailServiceApi {

    @ApiOperation(value = "Email Service", nickname = "sendEmail", notes = "", response = EmailResponseDTO.class, tags={ "email-service", })
    @ApiResponses(value = {
        @ApiResponse(code = 200, message = "OK", response = EmailResponseDTO.class) })
    @PostMapping(
        value = "/email",
        produces = { "application/json" },
        consumes = { "multipart/mixed" }
    )
    default ResponseEntity<EmailResponseDTO> sendEmail(@ApiParam(value = "") @Valid @RequestPart(value = "sendTo", required = false)  String sendTo,@ApiParam(value = "") @Valid @RequestPart(value = "sentFrom", required = false)  String sentFrom,@ApiParam(value = "") @Valid @RequestPart(value = "subject", required = false)  String subject,@ApiParam(value = "") @Valid @RequestPart(value = "content", required = false)  String content,@ApiParam(value = "") @Valid @RequestPart(value = "file", required = false) MultipartFile file) {
        return getDelegate().sendEmail(sendTo, sentFrom, subject, content, file);
    }
}

这里是OpenAPI生成的API Delegate class(注意参数都是可选的):

public interface EmailServiceApiDelegate {
    default ResponseEntity<EmailResponseDTO> sendEmail(Optional<String> sendTo,
        Optional<String> sentFrom,
        Optional<String> subject,
        Optional<String> content,
        MultipartFile file) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "{ \"errorMessage\" : \"errorMessage\", \"status\" : \"status\" }";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString);
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }
}

使用allOf将所有模式组合在一个中。 EmailRequestDTO的属性和内联定义的 object 的属性是作为参数而不是作为 DTO 生成的。 https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#allof

如果您不希望在委托中生成可选字符串作为参数,请删除useOptional配置。 https://openapi-generator.tech/docs/generators/spring/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM