I'm using Swagger with OAS3, since I need anyOf
support. I have an API call that can take one of 2 possible schemas, an account
or an address
. The schema works well in Swagger: it shows, and validates. But the example
value shows only the first schema. I created an examples
array, per the documentation, but I have no idea where to add it:
const accountSchema = {
description: 'schema for adding a new account',
type: 'object',
properties: {
account: {
type: 'object',
properties: {
userId: {type: 'number'},
platformId: {type: 'number'},
name: {type: 'string'},
key: {type: 'string'},
secret: {type: 'string'},
test: {type: 'boolean'},
},
required: ['userId', 'platformId', 'name', 'key', 'secret']
}
},
};
const ethereumAddressSchema = {
description: 'schema for adding a new ethereum address',
type: 'object',
properties: {
ethereum: {
type: 'object',
properties: {
userId: {type: 'number'},
name: {type: 'string'},
address: {type: 'string'},
chainId: {type: 'number'},
},
required: ['userId','name', 'address']
}
}
};
const examples = [
{
account: {
"userId": 0,
"platformId": 0,
"name": "string",
"key": "string",
"secret": "string",
"test": true
},
},
{
ethereum: {
"userId": 0,
"address": '0xfffffffffffffff',
"name": "string",
"chainId": 1,
}
}
];
const body = {
anyOf: [accountSchema, ethereumAddressSchema]
};
const response = {
type: 'object',
properties: {
accountId: {type: 'number'},
reason: {type: 'string'}
},
required: []
};
const addAccountSchema = {
description: 'Add a new account',
tags: ['account'],
produces: ['application/json'],
summary: 'Add a new account',
body,
response: {
200: {
description: 'Account request valid',
...response
},
404: {
description: 'Account request parameters not found',
...response
},
422: {
description: 'Account request invalid',
...response
}
}
};
module.exports = addAccountSchema;
Where should I add the examples
array, or is there a better way to show the user the 2 possible schemas in the UI?