Skip to content

Commit

Permalink
fix: sibling for openapi 3.1 (#2112)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk committed Aug 2, 2022
1 parent a35bb3f commit 0b1a790
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,21 @@ export class OpenAPIParser {
if (
mergeAsAllOf &&
keys.some(
k => !['description', 'title', 'externalDocs', 'x-refsStack', 'x-parentRefs'].includes(k),
k =>
![
'description',
'title',
'externalDocs',
'x-refsStack',
'x-parentRefs',
'readOnly',
'writeOnly',
].includes(k),
)
) {
const { description, title, readOnly, writeOnly, ...restSchema } = rest as OpenAPISchema;
return {
allOf: [resolved, rest],
allOf: [{ description, title, readOnly, writeOnly }, resolved, restSchema],
} as T;
} else {
// small optimization
Expand Down
79 changes: 79 additions & 0 deletions src/services/__tests__/models/Response.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { parseYaml } from '@redocly/openapi-core';
import { outdent } from 'outdent';

import { ResponseModel } from '../../models/Response';
import { OpenAPIParser } from '../../OpenAPIParser';
import { RedocNormalizedOptions } from '../../RedocNormalizedOptions';
Expand Down Expand Up @@ -53,5 +56,81 @@ describe('Models', () => {
expect(Object.keys(resp.extensions).length).toEqual(1);
expect(resp.extensions['x-example']).toEqual({ a: 1 });
});

test('should get correct sibling in responses for openapi 3.1', () => {
const spec = parseYaml(outdent`
openapi: 3.1.0
paths:
/test:
get:
operationId: test
responses:
'200':
description: Overridden description
$ref: "#/components/responses/Successful"
components:
responses:
Successful:
description: successful operation
content:
application/json:
schema:
type: object
properties:
successful:
type: boolean
`) as any;

parser = new OpenAPIParser(spec, undefined, opts);
const code = '200';
const responseModel = new ResponseModel({
parser: parser,
code: code,
defaultAsError: false,
infoOrRef: spec.paths['/test'].get.responses[code],
options: opts,
isEvent: false,
});

expect(responseModel.summary).toBe('Overridden description');
});

test('should not override description in responses for openapi 3.0', () => {
const spec = parseYaml(outdent`
openapi: 3.0.0
paths:
/test:
get:
operationId: test
responses:
'200':
description: Overridden description
$ref: "#/components/responses/Successful"
components:
responses:
Successful:
description: successful operation
content:
application/json:
schema:
type: object
properties:
successful:
type: boolean
`) as any;

parser = new OpenAPIParser(spec, undefined, opts);
const code = '200';
const responseModel = new ResponseModel({
parser: parser,
code: code,
defaultAsError: false,
infoOrRef: spec.paths['/test'].get.responses[code],
options: opts,
isEvent: false,
});

expect(responseModel.summary).toBe('successful operation');
});
});
});
81 changes: 81 additions & 0 deletions src/services/__tests__/models/Schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { parseYaml } from '@redocly/openapi-core';
import { outdent } from 'outdent';
import { MediaTypeModel } from '../../models';
import { SchemaModel } from '../../models/Schema';
import { OpenAPIParser } from '../../OpenAPIParser';
import { RedocNormalizedOptions } from '../../RedocNormalizedOptions';
Expand Down Expand Up @@ -481,5 +482,85 @@ describe('Models', () => {
`);
});
});

test('should get correct sibling inside schema type for openapi 3.1', () => {
const spec = parseYaml(outdent`
openapi: 3.1.0
paths:
/test:
get:
operationId: test
responses:
'200':
content:
application/json:
schema:
type: object
properties:
testAttr:
description: Overridden description
type: string
$ref: '#/components/schemas/Test'
components:
schemas:
Test:
type: object
description: Refed description
`) as any;

parser = new OpenAPIParser(spec, undefined, opts);
const name = 'application/json';
const mediaType = new MediaTypeModel(
parser,
name,
true,
spec.paths['/test'].get.responses['200'].content[name],
opts,
);

expect(printSchema(mediaType?.schema as any)).toMatchInlineSnapshot(
`"testAttr: <string> (Overridden description)"`,
);
});

test('should not override schema in openapi 3.0', () => {
const spec = parseYaml(outdent`
openapi: 3.0.0
paths:
/test:
get:
operationId: test
responses:
'200':
content:
application/json:
schema:
type: object
properties:
testAttr:
type: string
description: Overridden description
$ref: '#/components/schemas/Test'
components:
schemas:
Test:
type: object
description: Refed description
`) as any;

parser = new OpenAPIParser(spec, undefined, opts);
const name = 'application/json';
const mediaType = new MediaTypeModel(
parser,
name,
true,
spec.paths['/test'].get.responses['200'].content[name],
opts,
);

expect(printSchema(mediaType?.schema as any)).toMatchInlineSnapshot(
`"testAttr: <object> (Refed description)"`,
);
});
});
});

0 comments on commit 0b1a790

Please sign in to comment.