Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sibling for openapi 3.1 #2112

Merged
merged 1 commit into from
Aug 2, 2022
Merged
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
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)"`,
);
});
});
});