【问题标题】:Swagger Codegen: Inheritance and Composition not working as expectedSwagger Codegen:继承和组合没有按预期工作
【发布时间】:2016-09-19 17:18:41
【问题描述】:

我有以下简短的 YAML:

# Transaction Request object with minimal information that we need
  Parent:
    required:
  - a
  - b
  - c
properties:
  a:
    type: number
    format: double
  b:
    type: string
  c:
    type: string

# Full transaction
Child:
required:
  - a 
  - b
  - c
allOf:
  - $ref: "#/definitions/Parent"
properties:    
  date: 
    type: string 
    format: date-time
  state: 
    type: string 
    enum: 
      - 1
      - 2
      - 3

在 Swagger UI 和编辑器中,这些对象按我希望的方式显示:Child 继承了 Parentabc 字段,并且还有一些其他字段。

我早就料到了:

public class Parent {

  private Double a;
  private String b;
  private String c;

  ...}

 public class Child extends Parent {

 // Superclass fields as well as:
 private Date date;
 private enum State {...};

  ...}

然而,虽然 Parent 类看起来符合预期,但我的 Child 类包含以下内容:

public class Child   {



@Override
public boolean equals(Object o) {
if (this == o) {
  return true;
}
if (o == null || getClass() != o.getClass()) {
  return false;
}
Child child = (Child) o;
return true;
}

... }

甚至缺少extends。使用discriminator 时,它可以工作,但我并不真正想要多态性——只是简单的继承。如何使用 Swagger Codegen 完成此任务?


相关pom.xml条目:

            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.2.2-SNAPSHOT</version>

                <configuration>
                    <inputSpec>${project.basedir}/src/main/resources/test.yaml</inputSpec>
                    <language>jaxrs-resteasy</language>
                    <output>${project.build.directory}/generated-sources/payment</output>

                    <configOptions>
                        <sourceFolder>src/java/main</sourceFolder>
                        <dateLibrary>java8</dateLibrary>
                    </configOptions>

                    <groupId>net.product</groupId>
                    <artifactId>product_api</artifactId>
                    <modelPackage>net.product.product_api.model</modelPackage>
                    <invokerPackage>net.product.product_api</invokerPackage>
                    <apiPackage>net.product.product_api</apiPackage>
                </configuration>

                <executions>

                    <execution>
                        <id>generate-server-stubs</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                        </configuration>
                    </execution>

                </executions>

            </plugin>

【问题讨论】:

  • 您使用的是哪个版本?在 2.2.2-SNAPSHOT 上,它适用于 java 使用这个 yaml s3.amazonaws.com/genexuss3test/prueba.yaml
  • 我用的是2.1.6,后来也试过用2.2.1。我没有尝试过最新的开发版本,因为我们真的想使用稳定版本而不是 SNAPSHOT 来构建我们的存根。不仅如此,当我尝试输入2.2.2-SNAPSHOT作为版本时,maven也给我一个错误,说它的POM丢失了。
  • 我刚刚尝试将2.2.2-SNAPSHOT jar 手动安装到我的本地存储库中。然而,用它生成存根会产生与上面完全相同的结果。你能告诉我你用什么语言/框架生成存根吗?同时,我将使用我的 pom.xml 条目更新我的帖子。
  • 我检查了你提到的版本和当前主分支的版本,我得到了你的结果。出于某种原因,我 3 个月前从 master 克隆的那个几乎可以正常工作,here is the output。但是github上有一个issue可能与你的问题有关。

标签: java inheritance swagger swagger-codegen


【解决方案1】:
Cat:
  allOf:
  - $ref: "#/definitions/Animal"
  - type: "object"
    properties:
      declawed:
        type: "boolean"
Animal:
  type: "object"
  required:
  - "className"
  discriminator: "className"
  properties:
    className:
      type: "string"
    color:
      type: "string"
      default: "red"

你需要在父类添加代码

required:
    - "className"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    相关资源
    最近更新 更多