Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 217 additions & 0 deletions tools/spectral/ipa/__tests__/IPA103ResponseBodyRootShape.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-103-response-body-root-shape', [
{
name: 'valid object response bodies',
document: {
paths: {
'/resource/{id}': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-08-05+json': {
schema: {
type: 'object',
properties: {
name: { type: 'string' },
},
},
},
},
},
},
},
},
'/resource': {
get: {
responses: {
200: {
content: {
// Collection envelope per IPA-110
'application/vnd.atlas.2023-08-05+json': {
schema: {
type: 'object',
properties: {
results: { type: 'array', items: { type: 'object' } },
links: { type: 'array', items: { type: 'object' } },
totalCount: { type: 'integer' },
},
},
},
},
},
},
},
},
},
},
errors: [],
},
{
name: 'valid discriminated oneOf response body',
document: {
paths: {
'/resource/{id}': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-08-05+json': {
schema: {
oneOf: [
{ type: 'object', properties: { a: { type: 'string' } } },
{ type: 'object', properties: { b: { type: 'string' } } },
],
},
},
},
},
},
},
},
},
},
errors: [],
},
{
name: 'responses without a schema and non-JSON responses are ignored',
document: {
paths: {
'/resource/{id}': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-08-05+json': {
example: 'no schema',
},
'text/csv': {
schema: { type: 'array', items: { type: 'string' } },
},
},
},
},
},
},
},
},
errors: [],
},
{
name: 'invalid response bodies',
document: {
paths: {
'/resource': {
get: {
responses: {
200: {
content: {
// Array root
'application/vnd.atlas.2023-08-05+json': {
schema: { type: 'array', items: { type: 'object' } },
},
// Primitive root
'application/vnd.atlas.2024-08-05+json': {
schema: { type: 'string' },
},
},
},
},
},
},
'/resource/{id}': {
get: {
responses: {
200: {
content: {
// Object with only additionalProperties
'application/vnd.atlas.2023-08-05+json': {
schema: { type: 'object', additionalProperties: { type: 'string' } },
},
// oneOf with a non-object variant
'application/vnd.atlas.2024-08-05+json': {
schema: {
oneOf: [{ type: 'object', properties: { a: { type: 'string' } } }, { type: 'string' }],
},
},
},
},
},
},
},
},
},
errors: [
{
code: 'xgen-IPA-103-response-body-root-shape',
message:
'Response bodies must be a JSON object with named properties, or a oneOf whose variants are all objects.',
path: ['paths', '/resource', 'get', 'responses', '200', 'content', 'application/vnd.atlas.2023-08-05+json'],
severity: DiagnosticSeverity.Error,
},
{
code: 'xgen-IPA-103-response-body-root-shape',
message:
'Response bodies must be a JSON object with named properties, or a oneOf whose variants are all objects.',
path: ['paths', '/resource', 'get', 'responses', '200', 'content', 'application/vnd.atlas.2024-08-05+json'],
severity: DiagnosticSeverity.Error,
},
{
code: 'xgen-IPA-103-response-body-root-shape',
message:
'Response bodies must be a JSON object with named properties, or a oneOf whose variants are all objects.',
path: [
'paths',
'/resource/{id}',
'get',
'responses',
'200',
'content',
'application/vnd.atlas.2023-08-05+json',
],
severity: DiagnosticSeverity.Error,
},
{
code: 'xgen-IPA-103-response-body-root-shape',
message:
'Response bodies must be a JSON object with named properties, or a oneOf whose variants are all objects.',
path: [
'paths',
'/resource/{id}',
'get',
'responses',
'200',
'content',
'application/vnd.atlas.2024-08-05+json',
],
severity: DiagnosticSeverity.Error,
},
],
},
{
name: 'invalid response body with exception',
document: {
paths: {
'/resource': {
get: {
responses: {
200: {
content: {
'application/vnd.atlas.2023-08-05+json': {
schema: { type: 'array', items: { type: 'object' } },
'x-xgen-IPA-exception': {
'xgen-IPA-103-response-body-root-shape': 'Reason',
},
},
},
},
},
},
},
},
},
errors: [],
},
]);
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import testRule from './__helpers__/testRule';
import { DiagnosticSeverity } from '@stoplight/types';

testRule('xgen-IPA-117-oneof-property-descriptions-must-match', [
{
name: 'valid oneOf with matching shared property descriptions',
document: {
components: {
schemas: {
ExampleSchema: {
oneOf: [
{
type: 'object',
properties: {
id: { type: 'string', description: 'The identifier.' },
name: { type: 'string', description: 'The name.' },
},
},
{
type: 'object',
properties: {
id: { type: 'string', description: 'The identifier.' },
nickname: { type: 'string', description: 'The nickname.' },
},
},
],
},
},
},
},
errors: [],
},
{
name: 'valid oneOf with a single variant',
document: {
components: {
schemas: {
ExampleSchema: {
oneOf: [
{
type: 'object',
properties: {
id: { type: 'string', description: 'The identifier.' },
},
},
],
},
},
},
},
errors: [],
},
{
name: 'invalid oneOf with diverging shared property descriptions',
document: {
components: {
schemas: {
ExampleSchema: {
oneOf: [
{
type: 'object',
properties: {
id: { type: 'string', description: 'The identifier.' },
status: { type: 'string', description: 'The status.' },
},
},
{
type: 'object',
properties: {
id: { type: 'string', description: 'A unique identifier.' },
status: { type: 'string', description: 'The current status.' },
},
},
],
},
},
},
},
errors: [
{
code: 'xgen-IPA-117-oneof-property-descriptions-must-match',
message:
"Property 'id' has diverging descriptions across oneOf variants. Shared properties must use the same description.",
path: ['components', 'schemas', 'ExampleSchema'],
severity: DiagnosticSeverity.Warning,
},
{
code: 'xgen-IPA-117-oneof-property-descriptions-must-match',
message:
"Property 'status' has diverging descriptions across oneOf variants. Shared properties must use the same description.",
path: ['components', 'schemas', 'ExampleSchema'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid oneOf where one variant omits the description',
document: {
components: {
schemas: {
ExampleSchema: {
oneOf: [
{
type: 'object',
properties: {
id: { type: 'string', description: 'The identifier.' },
},
},
{
type: 'object',
properties: {
id: { type: 'string' },
},
},
],
},
},
},
},
errors: [
{
code: 'xgen-IPA-117-oneof-property-descriptions-must-match',
message:
"Property 'id' has diverging descriptions across oneOf variants. Shared properties must use the same description.",
path: ['components', 'schemas', 'ExampleSchema'],
severity: DiagnosticSeverity.Warning,
},
],
},
{
name: 'invalid oneOf with exception',
document: {
components: {
schemas: {
ExampleSchema: {
'x-xgen-IPA-exception': {
'xgen-IPA-117-oneof-property-descriptions-must-match': 'Reason',
},
oneOf: [
{
type: 'object',
properties: {
id: { type: 'string', description: 'The identifier.' },
},
},
{
type: 'object',
properties: {
id: { type: 'string', description: 'A different description.' },
},
},
],
},
},
},
},
errors: [],
},
]);
Loading
Loading