From 81c0df092aea266f002b4934687179d173a4cb05 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Fri, 24 Aug 2018 11:54:26 +0300 Subject: [PATCH] chore: add tests for one of titles --- .../__tests__/fixtures/oneOfTitles.json | 79 +++++++++++++++++++ src/services/__tests__/models/Schema.test.ts | 24 ++++++ 2 files changed, 103 insertions(+) create mode 100644 src/services/__tests__/fixtures/oneOfTitles.json diff --git a/src/services/__tests__/fixtures/oneOfTitles.json b/src/services/__tests__/fixtures/oneOfTitles.json new file mode 100644 index 00000000..68b0e4e4 --- /dev/null +++ b/src/services/__tests__/fixtures/oneOfTitles.json @@ -0,0 +1,79 @@ +{ + "openapi": "3.0.0", + "info": { + "version": "1.0", + "title": "Foo" + }, + "components": { + "schemas": { + "Test": { + "type": "object", + "properties": { + "any": { + "anyOf": [ + { + "$ref": "#/components/schemas/Foo" + }, + { + "$ref": "#/components/schemas/Bar" + } + ] + }, + "one": { + "oneOf": [ + { + "$ref": "#/components/schemas/Foo" + }, + { + "$ref": "#/components/schemas/Bar" + } + ] + }, + "all": { + "allOf": [ + { + "$ref": "#/components/schemas/Foo" + }, + { + "$ref": "#/components/schemas/Bar" + } + ] + } + } + }, + "Foo": { + "type": "object", + "properties": { + "foo": { + "type": "string" + } + } + }, + "Bar": { + "type": "object", + "properties": { + "bar": { + "type": "string" + } + } + }, + "WithArray": { + "oneOf": [{ + "type" : "array", + "items": { + "oneOf": [ + { + "type": "string" + }, + { + "type": "number" + } + ] + } + }, { + "type": "string" + }] + } + } + } +} \ No newline at end of file diff --git a/src/services/__tests__/models/Schema.test.ts b/src/services/__tests__/models/Schema.test.ts index 9c1c20fe..dc4b1d6e 100644 --- a/src/services/__tests__/models/Schema.test.ts +++ b/src/services/__tests__/models/Schema.test.ts @@ -15,5 +15,29 @@ describe('Models', () => { expect(schema.oneOf).toHaveLength(1); expect(schema.discriminatorProp).toEqual('type'); }); + + test('oneOf/allOf titles', () => { + const spec = require('../fixtures/oneOfTitles.json'); + parser = new OpenAPIParser(spec, undefined, opts); + const schema = new SchemaModel(parser, spec.components.schemas.Test, '', opts); + expect(schema.fields).toHaveLength(3); + const oneOfField = schema.fields[0]; + expect(oneOfField.schema.displayType).toBe('Foo (object) or Bar (object)'); + expect(oneOfField.schema.oneOf[0].title).toBe('Foo'); + expect(oneOfField.schema.oneOf[1].title).toBe('Bar'); + + const anyOfField = schema.fields[1]; + expect(anyOfField.schema.displayType).toBe('Foo (object) or Bar (object)'); + expect(anyOfField.schema.oneOf[0].title).toBe('Foo'); + expect(anyOfField.schema.oneOf[1].title).toBe('Bar'); + }); + + test('oneOf/allOf schema complex displayType', () => { + const spec = require('../fixtures/oneOfTitles.json'); + parser = new OpenAPIParser(spec, undefined, opts); + const schema = new SchemaModel(parser, spec.components.schemas.WithArray, '', opts); + expect(schema.oneOf).toHaveLength(2); + expect(schema.displayType).toBe('(Array of string or number) or string'); + }); }); });