gpt4 book ai didi

java - Swagger代码生成: Inheritance and Composition not working as expected

转载 作者:行者123 更新时间:2023-12-01 09:29:18 25 4
gpt4 key购买 nike

我有以下简短的 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 继承 abc来自 Parent 字段,并且还有一些其他字段。

我本来期望:

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

... }

甚至缺少扩展。当使用鉴别器时它可以工作,但我并不真正想要多态性 - 只是简单的继承。我如何使用 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>

最佳答案

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"

关于java - Swagger代码生成: Inheritance and Composition not working as expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39578692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com