如何告诉openapi-codegen使用超类而不是Object.class

时间:2019-12-16 15:20:35

标签: java spring jax-rs openapi openapi-generator

我正在使用openapi-codegen从yaml文件生成Java服务器API。我试图了解openapi使用继承的方式,为此,我从规范中获取了示例:
https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.1.md#discriminator-object

我遇到的问题是生成的代码(除了导入大量未使用的类之外​​)使用Java类Object.class而不是超类Pet.class作为返回类型。我在jaxrs中遇到了此行为,它返回了Object,而在spring服务器中返回的类型是ResponseEntity<Object>

这就是我生成API的方式:

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

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
          schema:
            oneOf:
            - $ref: '#/components/schemas/Cat'
            - $ref: '#/components/schemas/Dog'
            - $ref: '#/components/schemas/Lizard'
          discriminator:
            propertyName: pet_type
            mapping:
              cat: '#/components/schemas/Cat'
              dog: '#/components/schemas/Dog'
              lizard: '#/components/schemas/Lizard'
      responses:
        "200":
          description: Pet sucesfully returned
          content:
            application/json:
                schema:
                  oneOf:
                  - $ref: '#/components/schemas/Cat'
                  - $ref: '#/components/schemas/Dog'
                  - $ref: '#/components/schemas/Lizard'
                discriminator:
                  propertyName: pet_type
                  mapping:
                    dog: '#/components/schemas/Dog'
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
    Dog:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Dog`
        properties:
          bark:
            type: string
    Lizard:
      allOf:
      - $ref: '#/components/schemas/Pet'
      - type: object
        # all other properties specific to a `Lizard`
        properties:
          lovesRocks:
            type: boolean

春季生成的类:

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

import io.swagger.annotations.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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-16T16:16:21.319+01:00[Europe/Berlin]")

@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 = Object.class, tags={ "test", })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Pet sucesfully returned", response = Object.class) })
    @RequestMapping(value = "/echoPet",
        produces = { "application/json" }, 
        method = RequestMethod.POST)
    default ResponseEntity<Object> echoPet() {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

}

Jaxrs-cxf的生成类:

package org.openapitools.api;


import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.MediaType;
import org.apache.cxf.jaxrs.ext.multipart.*;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.ApiResponse;
import io.swagger.jaxrs.PATCH;
import javax.validation.constraints.*;
import javax.validation.Valid;

/**
 * Three Pets
 *
 * <p>No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
 *
 */
@Path("/")
@Api(value = "/", description = "")
public interface TestApi  {

    /**
     * Return input
     *
     * Send pet get pet
     *
     */
    @POST
    @Path("/echoPet")
    @Produces({ "application/json" })
    @ApiOperation(value = "Return input", tags={ "test" })
    @ApiResponses(value = { 
        @ApiResponse(code = 200, message = "Pet sucesfully returned", response = Object.class) })
    public Object echoPet();

1 个答案:

答案 0 :(得分:0)

您是否正在使用支持openapi 3.0的swagger-codegen版本?