OpenAPI (3.0) und Polymorphismus im KundenJava

Java-Forum
Anonymous
 OpenAPI (3.0) und Polymorphismus im Kunden

Post by Anonymous »

Ich versuche, die Erbschaft in OpenAPI zu modellieren. Letztendlich möchte ich DART -Kunden, aber nur dann erfolgreich für Java und nur dann für die 2.0 OpenAPI -Spezifikation < /p>
laufend: < /p>

Code: Select all

java -jar swagger-codegen-cli-3.0.68.jar generate -i swagger.yaml -l java -o ./_swagger
< /code>
mit: < /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>
Erzeugt < /p>
public class Dog { ...}
Aber mit dem folgenden (aus dem Arbeitsbeispiel in https://github.com/swagger-api/swagger- ... ssues/6148) und Verwenden von Definitionen (vs. Komponenten/Schemas) ...

Code: Select all

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>
Ich erhalte: < /p>
public class Dog extends Pet { }
< /code>
Wie generiere ich die Vererbung in 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
Das sieht aus wie die Vereinigung von Katze und Hund ... und kein tatsächlicher Code für die Deserialisierung einer Katze oder eines Hundes in den Laden.

Code: Select all

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

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post