add sample for multiple types union (#467)

This commit is contained in:
JianyeXi 2022-04-26 10:05:07 +08:00 коммит произвёл GitHub
Родитель 47ffbe9d58
Коммит 8cff4dcf25
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 198 добавлений и 69 удалений

Просмотреть файл

@ -0,0 +1,35 @@
import "@cadl-lang/rest";
import "@cadl-lang/openapi";
@serviceTitle("Pet Store Service")
@serviceVersion("2021-03-25")
namespace PetStore;
using Cadl.Http;
model ApiVersionParam {
@header apiVersion: string;
}
model PetBase {
name: string;
}
model Dog extends PetBase {
type: "dog";
nextWalkTime: zonedDateTime;
}
model Cat extends PetBase {
type: "cat";
catnipDose: int32;
}
union Pet {
cat: Cat,
dog: Dog,
}
@route("/")
interface MyService {
getPet(...ApiVersionParam): OkResponse<Pet>;
}

Просмотреть файл

@ -0,0 +1,115 @@
{
"openapi": "3.0.0",
"info": {
"title": "Pet Store Service",
"version": "2021-03-25"
},
"tags": [],
"paths": {
"/": {
"get": {
"operationId": "MyService_getPet",
"parameters": [
{
"$ref": "#/components/parameters/ApiVersionParam"
}
],
"responses": {
"200": {
"description": "The request has succeeded.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
}
}
}
}
},
"components": {
"parameters": {
"ApiVersionParam": {
"name": "apiVersion",
"in": "header",
"required": true,
"schema": {
"type": "string"
}
}
},
"schemas": {
"Pet": {
"anyOf": [
{
"$ref": "#/components/schemas/Cat"
},
{
"$ref": "#/components/schemas/Dog"
}
]
},
"Cat": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"cat"
]
},
"catnipDose": {
"type": "integer",
"format": "int32"
}
},
"required": [
"type",
"catnipDose"
],
"allOf": [
{
"$ref": "#/components/schemas/PetBase"
}
]
},
"Dog": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"dog"
]
},
"nextWalkTime": {
"type": "string",
"format": "date-time"
}
},
"required": [
"type",
"nextWalkTime"
],
"allOf": [
{
"$ref": "#/components/schemas/PetBase"
}
]
},
"PetBase": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
}
}
}
}

Просмотреть файл

@ -20,7 +20,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
"$ref": "#/components/schemas/PetBase"
}
}
}
@ -67,37 +67,13 @@
}
},
"schemas": {
"Pet": {
"PetBase": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"dog"
]
"description": "Discriminator property for PetBase."
},
"nextWalkTime": {
"type": "string",
"format": "date-time"
},
"walkerName": {
"type": "string"
}
},
"required": [
"type",
"nextWalkTime",
"walkerName"
],
"allOf": [
{
"$ref": "#/components/schemas/PetBase"
}
]
},
"PetBase": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
@ -109,6 +85,12 @@
"x-cadl-name": "Library.PetToy[]"
}
},
"discriminator": {
"propertyName": "type",
"mapping": {
"dog": "#/components/schemas/Dog"
}
},
"required": [
"name",
"favoriteToys"

Просмотреть файл

@ -20,7 +20,7 @@
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
"$ref": "#/components/schemas/PetBase"
}
}
}
@ -67,38 +67,34 @@
}
},
"schemas": {
"Pet": {
"anyOf": [
{
"$ref": "#/components/schemas/Cat"
},
{
"$ref": "#/components/schemas/Dog"
}
]
},
"Cat": {
"PetBase": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": [
"cat"
]
"description": "Discriminator property for PetBase."
},
"catnipDose": {
"type": "integer",
"format": "int32"
"name": {
"type": "string"
},
"favoriteToys": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Library.PetToy"
},
"x-cadl-name": "Library.PetToy[]"
}
},
"discriminator": {
"propertyName": "type",
"mapping": {
"dog": "#/components/schemas/Dog",
"cat": "#/components/schemas/Cat"
}
},
"required": [
"type",
"catnipDose"
],
"allOf": [
{
"$ref": "#/components/schemas/PetBase"
}
"name",
"favoriteToys"
]
},
"Dog": {
@ -136,23 +132,28 @@
}
]
},
"PetBase": {
"Cat": {
"type": "object",
"properties": {
"name": {
"type": "string"
"type": {
"type": "string",
"enum": [
"cat"
]
},
"favoriteToys": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Library.PetToy"
},
"x-cadl-name": "Library.PetToy[]"
"catnipDose": {
"type": "integer",
"format": "int32"
}
},
"required": [
"name",
"favoriteToys"
"type",
"catnipDose"
],
"allOf": [
{
"$ref": "#/components/schemas/PetBase"
}
]
},
"Library.PetToy": {

Просмотреть файл

@ -20,6 +20,7 @@ model ApiVersionParam {
@header apiVersion: VERSIONS;
}
@discriminator("type")
model PetBase {
name: string;
favoriteToys: Library.PetToy[];
@ -42,14 +43,9 @@ model Cat extends PetBase {
catnipDose: int32;
}
union Pet {
cat: Cat,
dog: Dog,
}
@route("/")
interface MyService {
getPet(...ApiVersionParam): OkResponse<Pet>;
getPet(...ApiVersionParam): OkResponse<PetBase>;
@added("v2")
@post