这是继承的示例工作代码:
class Link {
@Schema(description = "Next link", example = "http://")
@JsonProperty
private String next;
@Schema(description = "Previous link", example = "http://")
@JsonProperty
private String previous;
}
@Schema(subTypes = {SampleA.class, SampleB.class})
class Sample<T> {
@JsonProperty
private List<T> items;
@JsonProperty
private Link _href;
}
@Schema(allOf = Sample.class)
class SampleA extends Sample {}
@Schema(allOf = Sample.class)
class SampleB extends Sample {}
@RestController
public class HelloController {
@GetMapping("/getA")
SampleA getA() {
return new SampleA();
}
@GetMapping("/getB")
SampleB getAB() {
return new SampleB();
}
}
这是由此产生的 OpenAPI 规范:
openapi: 3.0.1
info:
title: OpenAPI definition
version: v0
servers:
- url: 'http://localhost:8080'
description: Generated server url
paths:
/getA:
get:
tags:
- hello-controller
operationId: getA
responses:
'200':
description: OK
content:
'*/*':
schema:
$ref: '#/components/schemas/SampleA'
/getB:
get:
tags:
- hello-controller
operationId: getAB
responses:
'200':
description: OK
content:
'*/*':
schema:
$ref: '#/components/schemas/SampleB'
components:
schemas:
Link:
type: object
properties:
next:
type: string
description: Next link
example: 'http://'
previous:
type: string
description: Previous link
example: 'http://'
Sample:
type: object
properties:
items:
type: array
items:
type: object
_href:
$ref: '#/components/schemas/Link'
SampleA:
type: object
allOf:
- $ref: '#/components/schemas/Sample'
SampleB:
type: object
allOf:
- $ref: '#/components/schemas/Sample'