用于生成不一致类的openapi maven插件

cngwdvgl  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(465)

我们使用openapi生成器maven插件版本5.0.1来生成API。我试图指定一个请求,其中包括一个dto和一个文件。
第一件奇怪的事情是,生成的代码没有使用dto,它基本上是平展字段,因此api期望每个字段都被指定。但是,我们并不太关心这个问题,因为我们可以指定每个字段(尽管如果它按预期工作会很好)。
杀死我们的问题是,为api和api委托生成的类彼此不一致。生成的api将每个字段视为 String . 但是,api委托将它们视为 Optional<String> . 因此,当我们试图编译代码时,api会得到一个编译错误,因为它将字符串传递给委托,委托需要 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类(注意,参数都是字符串):

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委托类(注意,参数都是可选的):

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);
    }
}
bgibtngc

bgibtngc1#

使用 allOf 将列出的所有架构合并在一起。的性质 EmailRequestDTO 而内联定义对象的那些对象是作为参数而不是作为dto生成的。看到了吗https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/#allof
如果不希望可选字符串作为委托中的参数生成,请删除 useOptional 配置。看到了吗https://openapi-generator.tech/docs/generators/spring/

相关问题