From c0ddbf32f40a8ce372794d5a0da06e517fb23a03 Mon Sep 17 00:00:00 2001 From: Andrea Carraro Date: Thu, 7 Sep 2023 16:44:35 +0200 Subject: [PATCH] test: add test for serialized strings (#62) --- test/fixtures/serialization/specs.yaml | 15 ++++++++++++ test/serialization.test.ts | 34 ++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 test/fixtures/serialization/specs.yaml create mode 100644 test/serialization.test.ts diff --git a/test/fixtures/serialization/specs.yaml b/test/fixtures/serialization/specs.yaml new file mode 100644 index 0000000..c9470fd --- /dev/null +++ b/test/fixtures/serialization/specs.yaml @@ -0,0 +1,15 @@ +openapi: 3.0.3 +info: + title: title + description: description + version: 1.0.0 +components: + schemas: + Foo: + description: Foo description + type: object + properties: + one: + type: 'string' + pattern1: '([a-zA-Z\s\-'']){1,50}' + pattern2: ([a-zA-Z\s\-']){1,50} diff --git a/test/serialization.test.ts b/test/serialization.test.ts new file mode 100644 index 0000000..3b07edd --- /dev/null +++ b/test/serialization.test.ts @@ -0,0 +1,34 @@ +import path from 'path'; +import { describe, it, expect } from 'vitest'; +import { openapiToTsJsonSchema } from '../src'; +import { importFresh } from './test-utils'; + +const fixtures = path.resolve(__dirname, 'fixtures'); + +describe('openapiToTsJsonSchema', async () => { + it('serializes strings as expected', async () => { + const { outputPath } = await openapiToTsJsonSchema({ + openApiSchema: path.resolve(fixtures, 'serialization/specs.yaml'), + definitionPathsToGenerateFrom: ['components.schemas'], + silent: true, + }); + + const actual = await importFresh( + path.resolve(outputPath, 'components/schemas/Foo'), + ); + + const expected = { + description: 'Foo description', + type: 'object', + properties: { + one: { + type: 'string', + pattern1: "([a-zA-Z\\s\\-']){1,50}", + pattern2: "([a-zA-Z\\s\\-']){1,50}", + }, + }, + }; + + expect(actual.default).toEqual(expected); + }); +});