Code: Select all
@JsonTypeInfo( property = "type", include = JsonTypeInfo.As.PROPERTY, use = JsonTypeInfo.Id.NAME )
@JsonSubTypes( { @JsonSubTypes.Type( FooWidget.class ), @JsonSubTypes.Type( BarWidget.class ) } )
public interface Widget {}
public class BarWidget implements Widget { private String bar; }
public class FooWidget implements Widget { private String foo; }
public class WidgetGroup
{
private List widgets;
}
@RestController
public class WidgetController
{
@PostMapping( "/widgets" )
public void createWidgets( @RequestBody WidgetGroup widgets )
{
}
}
- den obigen Code als Server-Stubs als auch als Modelle reproduzieren kann
- einen Javascript-Client erzeugen kann, der die Modelle und die API korrekt darstellt
Code: Select all
openapi: 3.0.1
info:
title: OpenAPI definition
version: v0
servers:
- url: 'http://localhost:8080'
description: Generated server url
paths:
/widgets:
post:
tags:
- widget-controller
operationId: createWidgets
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/WidgetGroup'
required: true
responses:
'200':
description: OK
components:
schemas:
BarWidget:
type: object
allOf:
- $ref: '#/components/schemas/Widget'
- type: object
properties:
bar:
type: string
FooWidget:
type: object
allOf:
- $ref: '#/components/schemas/Widget'
- type: object
properties:
foo:
type: string
Widget:
required:
- type
type: object
properties:
type:
type: string
discriminator:
propertyName: type
WidgetGroup:
type: object
properties:
widgets:
type: array
items:
oneOf:
- $ref: '#/components/schemas/BarWidget'
- $ref: '#/components/schemas/FooWidget'
Mobile version