简体   繁体   English

您如何让Maven swagger Codegen插件使用继承生成Java类?

[英]How do you have the maven swagger codegen plugin generate Java classes using inheritance?

In our swagger.yaml file, we have a definition Cat that uses allOf to include all properties of Pet . 在我们的swagger.yaml文件中,我们有一个定义Cat ,该定义使用allOf包含Pet所有属性。

Cat:
  allOf:
    - $ref: '#/definitions/Pet'
    - type: object
      properties:
      # ...

The expectation is that when generating Java sources, we get 期望是在生成Java源代码时,我们得到

public class Cat extends Pet {

This works when using the Swagger Editor . 使用Swagger编辑器时可以使用。

When using swagger-codegen-maven-plugin with no configOptions set, we get the following: 当使用未设置configOptions swagger-codegen-maven-plugin时,我们得到以下信息:

public class Cat {

Cat implements all of Pet 's properties itself, instead of extending it. Cat本身会实现Pet的所有属性,而不是对其进行扩展。

How do you tell the swagger-codegen-maven-plugin to use Java with inheritance (ie extends )? 您如何告诉swagger-codegen-maven-plugin使用Java进行继承(即extends )? (We tried both spring and java as languages.) (我们尝试使用spring和java作为语言。)

Here's a sample swagger.yaml file: 这是一个示例swagger.yaml文件:

swagger: '2.0'

info:
  version: 1.0.0
  title: simple inheritance

tags:
  - name: "pet"

paths:
  /cat:
    put:
      tags:
        - "pet"
      operationId: "updateCat"
      consumes:
        - "application/json"
      parameters:
        - in: "body"
          name: "body"
          required: true
          schema:
            $ref: "#/definitions/Cat"
      responses:
        200:
          description: Nada

definitions:
  Pet:
    type: "object"
    required:
      - "name"
    properties:
      name:
        type: "string"
        example: "doggie"
  Cat:
    allOf:
      - $ref: '#/definitions/Pet'
      - type: object
        properties:
          huntingSkill:
            type: string
        required:
          - huntingSkill

As pointed out on github by chiochuan , the workaround is to add 正如chiochuan在github上指出的,解决方法是添加

   discriminator: "type"

to the parent's definition to make the Java child classes extend it. 到父级的定义以使Java子类对其进行扩展。

definitions:
  Pet:
    type: "object"
    discriminator: "type"
    required:
      - "name"
    properties:
      name:
        type: "string"
        example: "doggie"

It's a bit strange as the OpenAPI Specification Version 2.0 state that the discriminator Fixed Field must reference a property from the same schema, and that it must be a required property, both of which isn't the case - but it works, at least as of today. 有点奇怪,因为OpenAPI规范版本2.0声明discriminator固定字段必须引用同一模式中的属性,并且它必须是required属性,但事实并非如此-但这两者都可以,至少今天的。 :) :)

暂无
暂无

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

相关问题 如何使用 swagger-codegen-maven 插件在 boolean 上生成 getter - how to generate getter on boolean using swagger-codegen-maven plugin 如何使用 swagger-codegen-plugin (maven) 生成客户端代码? - How to generate client code using with swagger-codegen-plugin (maven)? 如何使用 springdoc-openapi-maven-plugin 和 swagger-codegen-maven-plugin 生成客户端代码? - How to generate client code using springdoc-openapi-maven-plugin and swagger-codegen-maven-plugin? 如何使用maven-swagger-codegen-plugin生成文档? - How generate documentation with maven-swagger-codegen-plugin? 如何使用swagger-codegen-maven-plugin生成maven存储库就绪库? - How can I generate a maven repository-ready library with swagger-codegen-maven-plugin? maven-cxf-codegen-plugin 使用 Jaxb 绑定为所有生成的类添加继承 - maven-cxf-codegen-plugin using Jaxb binding to add inheritance for all generated classes 生成 java 类到 src 不仅要以 maven cxf-codegen-plugin 为目标 - Generate java classes to src not only to target with maven cxf-codegen-plugin 使用swagger codegen插件通过几个yaml文件生成代码 - generate code via several yaml file using swagger codegen plugin 我如何强制为Javaee 5生成cxf-codegen-plugin(CXF wsdltojava)Maven插件? - How do i force the cxf-codegen-plugin (CXF wsdltojava) Maven plugin to generate for Javaee 5? Swagger Codegen,Maven插件:限制服务器生成 - Swagger Codegen, Maven Plugin: Restrict Server Generation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM