[英]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.