Skip to content

Commit

Permalink
fix: property with nested allOf (#2083)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk authored Jul 19, 2022
1 parent 1a1bc26 commit 7cc0500
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
],
"schema": Object {
"allOf": undefined,
"description": undefined,
"discriminator": Object {
"propertyName": "type",
},
Expand All @@ -858,11 +859,13 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
],
},
},
"readOnly": undefined,
"required": Array [
"type",
],
"title": "Dog",
"type": "object",
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-parentRefs": Array [
"#/components/schemas/Pet",
Expand Down Expand Up @@ -1699,6 +1702,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
],
"schema": Object {
"allOf": undefined,
"description": undefined,
"discriminator": Object {
"propertyName": "type",
},
Expand All @@ -1717,11 +1721,13 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
],
},
},
"readOnly": undefined,
"required": Array [
"type",
],
"title": "Cat",
"type": "object",
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-parentRefs": Array [
"#/components/schemas/Pet",
Expand Down Expand Up @@ -2811,6 +2817,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
],
"schema": Object {
"allOf": undefined,
"description": undefined,
"discriminator": Object {
"propertyName": "type",
},
Expand All @@ -2826,11 +2833,13 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
],
},
},
"readOnly": undefined,
"required": Array [
"type",
],
"title": "Dog",
"type": "object",
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-parentRefs": Array [
"#/components/schemas/Pet",
Expand Down
6 changes: 6 additions & 0 deletions src/services/OpenAPIParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ export class OpenAPIParser {
items,
required,
title,
description,
readOnly,
writeOnly,
oneOf,
anyOf,
'x-circular-ref': isCircular,
Expand Down Expand Up @@ -305,6 +308,9 @@ export class OpenAPIParser {
receiver = {
...receiver,
title: receiver.title || title,
description: receiver.description || description,
readOnly: receiver.readOnly !== undefined ? receiver.readOnly : readOnly,
writeOnly: receiver.writeOnly !== undefined ? receiver.writeOnly : writeOnly,
'x-circular-ref': receiver['x-circular-ref'] || isCircular,
...otherConstraints,
};
Expand Down
52 changes: 52 additions & 0 deletions src/services/__tests__/models/RequestBody.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { parseYaml } from '@redocly/openapi-core';
import { outdent } from 'outdent';
import { RequestBodyModel } from '../../models/RequestBody';
import { OpenAPIParser } from '../../OpenAPIParser';
import { RedocNormalizedOptions } from '../../RedocNormalizedOptions';
Expand All @@ -23,5 +25,55 @@ describe('Models', () => {
expect(consoleError).not.toHaveBeenCalled();
expect(req).toEqual({ description: '', required: false });
});

test('should have correct field data when it includes allOf', () => {
const spec = parseYaml(outdent`
openapi: 3.0.0
paths:
/user:
post:
tags:
- user
summary: Create user
description: This can only be done by the logged in user.
operationId: createUser
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/User'
description: Created user object
required: true
components:
schemas:
User:
allOf:
- type: object
properties:
name:
type: string
description: correct description name
readOnly: false
writeOnly: false
allOf:
- $ref: '#/components/schemas/NameField'
NameField:
description: name description
readOnly: true
writeOnly: true
`) as any;

parser = new OpenAPIParser(spec, undefined, opts);
const req = new RequestBodyModel({
parser,
infoOrRef: spec.paths['/user'].post.requestBody,
options: opts,
isEvent: false,
});
const nameField = req.content?.mediaTypes[0].schema?.fields?.[0];
expect(nameField?.schema.readOnly).toBe(false);
expect(nameField?.schema.writeOnly).toBe(false);
expect(nameField?.description).toMatchInlineSnapshot(`"correct description name"`);
});
});
});
43 changes: 43 additions & 0 deletions src/services/__tests__/models/Schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { parseYaml } from '@redocly/openapi-core';
import { outdent } from 'outdent';
import { SchemaModel } from '../../models/Schema';
import { OpenAPIParser } from '../../OpenAPIParser';
import { RedocNormalizedOptions } from '../../RedocNormalizedOptions';
import { printSchema } from './helpers';

const opts = new RedocNormalizedOptions({});

Expand Down Expand Up @@ -240,5 +243,45 @@ describe('Models', () => {
},
);
});

test('should get correct fields data if it includes allOf', () => {
const spec = parseYaml(outdent`
openapi: 3.0.0
components:
schemas:
User:
allOf:
- type: object
properties:
name:
type: string
description: correct description name
readOnly: false
writeOnly: false
allOf:
- '#/components/schemas/NameField'
NameField:
type: object
description: name description
readOnly: true
writeOnly: false
`) as any;

parser = new OpenAPIParser(spec, undefined, opts);
const schema = new SchemaModel(
parser,
spec.components.schemas.User,
'#/components/schemas/User',
opts,
);
const fieldSchema = schema.fields?.[0].schema;
expect(fieldSchema?.readOnly).toBe(false);
expect(fieldSchema?.writeOnly).toBe(false);
expect(printSchema(schema)).toMatchInlineSnapshot(`
"name: <string> (correct description name)
allOf: <any>"
`);
});
});
});
23 changes: 23 additions & 0 deletions src/services/__tests__/models/__snapshots__/Schema.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ exports[`Models Schema schemaDefinition should resolve field with conditional op
Object {
"allOf": undefined,
"default": undefined,
"description": undefined,
"items": Object {
"allOf": undefined,
"description": undefined,
"format": "url",
"readOnly": undefined,
"title": undefined,
"type": "string",
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-parentRefs": Array [],
},
"maxItems": 20,
"minItems": 1,
"readOnly": undefined,
"title": "isString",
"type": "string",
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-displayName": "isString",
"x-parentRefs": Array [],
Expand All @@ -26,23 +32,29 @@ exports[`Models Schema schemaDefinition should resolve field with conditional op
Object {
"allOf": undefined,
"default": undefined,
"description": undefined,
"items": Object {
"allOf": undefined,
"description": undefined,
"format": "url",
"readOnly": undefined,
"title": undefined,
"type": "string",
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-parentRefs": Array [],
},
"maxItems": 10,
"minItems": 1,
"pattern": "\\\\d+",
"readOnly": undefined,
"title": "notString",
"type": Array [
"string",
"integer",
"null",
],
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-displayName": "notString",
"x-parentRefs": Array [],
Expand All @@ -52,6 +64,7 @@ Object {
exports[`Models Schema schemaDefinition should resolve schema with conditional operators 1`] = `
Object {
"allOf": undefined,
"description": undefined,
"maxItems": 2,
"properties": Object {
"test": Object {
Expand All @@ -62,29 +75,36 @@ Object {
],
"items": Object {
"allOf": undefined,
"description": undefined,
"format": "url",
"readOnly": undefined,
"title": undefined,
"type": "string",
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-parentRefs": Array [],
},
"maxItems": 20,
"minItems": 1,
"readOnly": undefined,
"title": undefined,
"type": Array [
"string",
"integer",
"null",
],
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-parentRefs": Array [],
"x-refsStack": Array [
"/oneOf/0",
],
},
},
"readOnly": undefined,
"title": "=== 10",
"type": "object",
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-parentRefs": Array [],
}
Expand All @@ -93,6 +113,7 @@ Object {
exports[`Models Schema schemaDefinition should resolve schema with conditional operators 2`] = `
Object {
"allOf": undefined,
"description": undefined,
"maxItems": 20,
"properties": Object {
"test": Object {
Expand All @@ -113,8 +134,10 @@ Object {
],
},
},
"readOnly": undefined,
"title": "case 2",
"type": "object",
"writeOnly": undefined,
"x-circular-ref": undefined,
"x-parentRefs": Array [],
}
Expand Down

0 comments on commit 7cc0500

Please sign in to comment.