Я пытаюсь моделировать наследование в OpenAPI 3. Я не понимаю, почему перемещение модельных определений из определений: в компоненты: схемы: вызывает наследство больше не моделируется. В конечном счете, я бы хотел клиентов DART, но успешно выполнил только наследство для Java, и только тогда для 2.0 OpenApi Spec. < /P>
работа: < /p>
java -jar swagger-codegen-cli-3.0.68.jar generate -i swagger.yaml -l java -o ./_swagger
< /code>
с: < /p>
components:
schemas:
Pet:
discriminator: petType
required:
- name
- petType # required for inheritance to work
properties:
name:
type: string
petType:
type: string
Cat:
allOf:
- $ref: '#/definitions/Pet' # Cat has all properties of a Pet
- properties: # extra properties only for cats
huntingSkill:
type: string
default: lazy
enum:
- lazy
- aggressive
Dog:
allOf:
- $ref: '#/definitions/Pet' # Dog has all properties of a Pet
- properties: # extra properties only for dogs
packSize:
description: The size of the pack the dog is from
type: integer
openapi: 3.0.3
security: []
servers: []
paths:
< /code>
производит < /p>
public class Dog { ...}
Но со следующим (взято из рабочего примера в https://github.com/swagger-api/swagger- ... ssues/6148) и использует определения (против компонентов/схемы) ...
swagger: "2.0"
basePath: "/v2"
definitions:
Pet:
discriminator: petType
required:
- name
- petType # required for inheritance to work
properties:
name:
type: string
petType:
type: string
Cat:
allOf:
- $ref: '#/definitions/Pet' # Cat has all properties of a Pet
- properties: # extra properties only for cats
huntingSkill:
type: string
default: lazy
enum:
- lazy
- aggressive
Dog:
allOf:
- $ref: '#/definitions/Pet' # Dog has all properties of a Pet
- properties: # extra properties only for dogs
packSize:
description: The size of the pack the dog is from
type: integer
< /code>
Я получаю: < /p>
public class Dog extends Pet { }
< /code>
Как меня генерировать наследование в OpenApi3?public class Dog extends Pet { }
< /code>
components:
schemas:
Store:
type: object
properties:
pet:
oneOf:
- $ref: "#/components/schemas/Cat"
- $ref: "#/components/schemas/Dog"
discriminator:
propertyName: petType
mapping:
Cat: "#/components/schemas/Cat"
Dog: "#/components/schemas/Dog"
Pet:
discriminator:
propertyName: petType
required:
- name
- petType # required for inheritance to work
properties:
name:
type: string
petType:
type: string
Cat:
allOf:
- $ref: '#/components/schemas/Pet' # Cat has all properties of a Pet
- properties: # extra properties only for cats
huntingSkill:
type: string
default: lazy
enum:
- lazy
- aggressive
Dog:
allOf:
- $ref: '#/components/schemas/Pet' # Dog has all properties of a Pet
- properties: # extra properties only for dogs
packSize:
description: The size of the pack the dog is from
type: integer
openapi: 3.0.3
security: []
servers: []
paths: {}
info:
version: 1.0.0
title: Swagger Petstore
< /code>
With the Dart client, there's a class StorePet, который выглядит как союз кошки и собаки ... и никакого фактического кода, не подходящего для десеризации кошки или собаки в store.pet на основе Pet.pettype.// java -jar openapi-generator-cli.jar generate -i swagger.yaml -g dart -o ./_swagger
class Store {
StorePet? pet; // Could have been Pet? Or Object?
...
}
class Pet {
String name;
String petType;
...
}
class Dog { // "extends Pet" is missing !!!
...
}
class StorePet {
...
String name;
String petType;
StorePetHuntingSkillEnum huntingSkill;
int? packSize;
...
}
< /code>
With a Java client target and the same specification, it looks a little better...
// java -jar openapi-generator-cli.jar generate -i swagger.yaml -g java -o ./_swagger
public class Store {
public static final String SERIALIZED_NAME_PET = "pet";
@SerializedName(SERIALIZED_NAME_PET)
private StorePet pet; ///
Подробнее здесь: https://stackoverflow.com/questions/796 ... the-client