为什么openapi-generator使用不存在的导入

时间:2019-12-18 10:38:21

标签: java spring swagger openapi openapi-generator

我正在使用openapi-codegen version 4.2.2openapi specification 3.0.0从yaml文件生成Java服务器API,如下所示:

openapi-generator generate -i test.yaml -g spring -o /tmp/springTest

我遇到的问题是,生成的代码试图导入一个不存在的类。 API应该接受pet类型的对象并返回相同的pet。宠物应该是猫,狗或蜥蜴。我使用了来自openapi规范的继承示例,并根据该示例构建了以下yaml文件:

openapi: 3.0.0
info:
  version: 1.0.0
  title: Three Pets
  termsOfService: http://swagger.io/terms
tags:
  - name: test
paths:
  /echoPet:
    post:
      tags:
        - test
      summary: Return input
      description: Send pet get pet
      operationId: echoPet
      parameters:
        - name: pet
          in: query
          schema:
            oneOf:
              - $ref: '#/components/schemas/Cat'
              - $ref: '#/components/schemas/Dog'
              - $ref: '#/components/schemas/Lizard'
      responses:
        "200":
          description: Pet sucesfully returned
          content:
            application/json:
                schema:
                  oneOf:
                    - $ref: '#/components/schemas/Pet'
servers:
  - url: https://localhost:8080/v2
  - url: http://localhost:8080/v2
components:
  schemas:
    Pet:
      type: object
      required:
      - pet_type
      properties:
        pet_type:
          type: string
      discriminator:
        propertyName: pet_type
        mapping:
          cachorro: Dog
    Cat:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Cat`
        properties:
          name:
            type: string
          huntingSkill:
            type: string
            description: The measured skill for hunting
            enum:
            - clueless
            - lazy
            - adventurous
            - aggressive
    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
          packSize:
            type: integer
            format: int32
            description: the size of the pack the dog is from
            default: 0
            minimum: 0
    Lizard:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Lizard`
        properties:
          lovesRocks:
            type: boolean

https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#discriminator-object

据我了解,它应该生成一个API,以支持以下Cat,Dog,Lizard类型之一。 (如果我在这里使用Pet,则验证将显示Pet不是对象类型) 生成的EchoPetApi.java将导入用于:

import org.openapitools.model.OneOfCatDogLizard;
import org.openapitools.model.OneOfPet;

但是这些类不存在,这将使代码不可编译。我在org.openapitools.model软件包中拥有的全部是:

Cat.java
CatAllOf.java
Dog.java
DogAllOf.java
Lizard.java
LizardAllOf.java
Pet.java

这里的Cat,Dog和Lizard.java看起来像预期的一样,并且都扩展了Pet.java,但是CatAllOf,DogAllOf和LizardAllOf并没有扩展任何东西,只具有各自类的属性和略有不同的hashCode()方法。 / p>

这是EchoPetApi.java,其中不存在导入(前两个,包之后):

/**
 * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech) (4.2.2).
 * https://openapi-generator.tech
 * Do not edit the class manually.
 */
package org.openapitools.api;

import org.openapitools.model.OneOfCatDogLizard;
import org.openapitools.model.OneOfPet;
import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;
@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2019-12-18T11:27:04.474+01:00[Europe/Berlin]")

@Validated
@Api(value = "echoPet", description = "the echoPet API")
public interface EchoPetApi {

    default Optional<NativeWebRequest> getRequest() {
        return Optional.empty();
    }

    @ApiOperation(value = "Return input", nickname = "echoPet", notes = "Send pet get pet", response = OneOfPet.class, tags={ "test", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Pet sucesfully returned", response = OneOfPet.class) })
    @RequestMapping(value = "/echoPet",
        produces = { "application/json" }, 
        method = RequestMethod.POST)
    default ResponseEntity<OneOfPet> echoPet(@ApiParam(value = "", defaultValue = "null") @Valid OneOfCatDogLizard pet) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

}

如果我使用openapi-codegen中的validate函数,它不会返回任何错误:

$ openapi-generator validate -i test.yaml
Validating spec (test.yaml)
No validation issues detected.

0 个答案:

没有答案