From 4332d2b4fc929aeb5a7cc371992d100c73817bbe Mon Sep 17 00:00:00 2001 From: Mikhail Marukhnenko Date: Tue, 23 Jan 2024 14:27:32 +0300 Subject: [PATCH] support length limits --- src/__snapshots__/length.test.ts.snap | 92 +++++++++++++++++++++++++++ src/__tests__/length.test.ts | 63 ++++++++++++++++++ src/includer/traverse/tables.ts | 13 ++++ 3 files changed, 168 insertions(+) create mode 100644 src/__snapshots__/length.test.ts.snap create mode 100644 src/__tests__/length.test.ts diff --git a/src/__snapshots__/length.test.ts.snap b/src/__snapshots__/length.test.ts.snap new file mode 100644 index 0000000..63a8720 --- /dev/null +++ b/src/__snapshots__/length.test.ts.snap @@ -0,0 +1,92 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`length renders correct length limits 1`] = ` +"
+ +# length + +## Request + +
+ +
+ +POST {.openapi__method} + + +\`\`\` +http://localhost:8080/test +\`\`\` + + +
+ +
+ +Generated server url{.openapi__request__description} + +## Responses + +
+ +## 200 OK + +### Body + +{% cut "application/json" %} + + +\`\`\`json +{ + "pet": { + "name": "string", + "foo": "string" + }, + "petWithoutDescription": { + "name": "string", + "foo": "string" + }, + "refToSchemaWithDescription": { + "name": "string", + "bar": "string" + } +} +\`\`\` + + +{% endcut %} + + +#||| **Name** | **Type** | **Description** || + +|| pet | [Cat](#cat) | From response || + +|| petWithoutDescription | [Cat](#cat) | Cat class || + +|| refToSchemaWithDescription | [Dog](#dog) | Dog class |||# + +### Cat + +Cat class + +#||| **Name** | **Type** | **Description** || + +|| name | string | || + +|| foo | string | Min length: \`3\` |||# + +### Dog + +Dog class + +#||| **Name** | **Type** | **Description** || + +|| name | string | Pet name
Max length: \`100\` || + +|| bar | string | Min length: \`1\`
Max length: \`99\` |||# + +
+ + +
" +`; diff --git a/src/__tests__/length.test.ts b/src/__tests__/length.test.ts new file mode 100644 index 0000000..9c3d5ba --- /dev/null +++ b/src/__tests__/length.test.ts @@ -0,0 +1,63 @@ +import {DocumentBuilder, run} from './__helpers__/run'; + +const name = 'length'; +describe('length', () => { + it('renders correct length limits', async () => { + const spec = new DocumentBuilder(name) + .component('Pet', { + allOf: [DocumentBuilder.ref('Cat', 'From allOf in Pet')], + description: 'From pet', + }) + .component('Cat', { + type: 'object', + properties: { + name: { + type: 'string', + }, + foo: { + type: 'string', + minLength: 3, + }, + }, + description: 'Cat class', + }) + .component('Dog', { + type: 'object', + properties: { + name: { + type: 'string', + description: 'Pet name', + maxLength: 100, + }, + bar: { + type: 'string', + minLength: 1, + maxLength: 99, + }, + }, + description: 'Dog class', + }) + .response(200, { + schema: { + properties: { + pet: { + allOf: [DocumentBuilder.ref('Pet', 'From response')], + }, + petWithoutDescription: { + allOf: [DocumentBuilder.ref('Cat')], + }, + refToSchemaWithDescription: { + allOf: [DocumentBuilder.ref('Dog')], + }, + }, + }, + }) + .build(); + + const fs = await run(spec); + + const page = fs.match(name); + + expect(page).toMatchSnapshot(); + }); +}); diff --git a/src/includer/traverse/tables.ts b/src/includer/traverse/tables.ts index e08aa94..04cc47e 100644 --- a/src/includer/traverse/tables.ts +++ b/src/includer/traverse/tables.ts @@ -209,6 +209,19 @@ function prepareComplexDescription(baseDescription: string, value: OpenJSONSchem ); } + if (typeof value.minLength !== 'undefined') { + description = concatNewLine( + description, + `Min length: \`${value.minLength}\``, + ); + } + if (typeof value.maxLength !== 'undefined') { + description = concatNewLine( + description, + `Max length: \`${value.maxLength}\``, + ); + } + return description; }