diff --git a/src/__snapshots__/basic.test.ts.snap b/src/__snapshots__/basic.test.ts.snap index 56d1c0e..c7e7bf8 100644 --- a/src/__snapshots__/basic.test.ts.snap +++ b/src/__snapshots__/basic.test.ts.snap @@ -63,19 +63,13 @@ Base 200 response || foo | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -115,19 +109,13 @@ Base 200 response || bar | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# diff --git a/src/__snapshots__/combiners/allOf.test.ts.snap b/src/__snapshots__/combiners/allOf.test.ts.snap index b660788..9693f19 100644 --- a/src/__snapshots__/combiners/allOf.test.ts.snap +++ b/src/__snapshots__/combiners/allOf.test.ts.snap @@ -56,28 +56,19 @@ Generated server url || foo | - **Type:** string - - - + **Type:** string || || name | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -117,10 +108,7 @@ Base 200 response || baz | - **Type:** string - - - + **Type:** string || || @@ -134,10 +122,7 @@ Base 200 response || type | - **Type:** string - - - + **Type:** string |||# @@ -207,8 +192,7 @@ Generated server url | **Type:** [Cat](#cat) -Cat class - +Cat class |||# @@ -228,19 +212,13 @@ Cat class || foo | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -284,8 +262,7 @@ Base 200 response | **Type:** [Cat](#cat) -Cat class - +Cat class |||# diff --git a/src/__snapshots__/combiners/complex.test.ts.snap b/src/__snapshots__/combiners/complex.test.ts.snap index 0b78406..599c727 100644 --- a/src/__snapshots__/combiners/complex.test.ts.snap +++ b/src/__snapshots__/combiners/complex.test.ts.snap @@ -59,10 +59,7 @@ Generated server url || age | - **Type:** any - - - + **Type:** any || || @@ -106,19 +103,13 @@ Dog class || bar | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -138,19 +129,13 @@ Cat class || foo | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -194,10 +179,7 @@ Base 200 response || age | - **Type:** any - - - + **Type:** any || || diff --git a/src/__snapshots__/combiners/oneOf.test.ts.snap b/src/__snapshots__/combiners/oneOf.test.ts.snap index 3eb6314..79c7d87 100644 --- a/src/__snapshots__/combiners/oneOf.test.ts.snap +++ b/src/__snapshots__/combiners/oneOf.test.ts.snap @@ -85,19 +85,13 @@ Dog class || baz | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -117,19 +111,13 @@ Cat class || foo | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -245,19 +233,13 @@ Generated server url || age | - **Type:** number - - - + **Type:** number || || name | - **Type:** string - - - + **Type:** string || || @@ -293,19 +275,13 @@ Dog class || baz | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -325,19 +301,13 @@ Cat class || foo | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -377,19 +347,13 @@ Cat class || age | - **Type:** number - - - + **Type:** number || || name | - **Type:** string - - - + **Type:** string || || @@ -476,10 +440,7 @@ Generated server url pet | **Type:** [Dog](#dog) -or [Cat](#cat) - - - +or [Cat](#cat) || || @@ -515,19 +476,13 @@ Dog class || baz | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -547,19 +502,13 @@ Cat class || foo | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -602,10 +551,7 @@ Cat class pet | **Type:** [Dog](#dog) -or [Cat](#cat) - - - +or [Cat](#cat) || || diff --git a/src/__snapshots__/constraints/default.test.ts.snap b/src/__snapshots__/constraints/default.test.ts.snap new file mode 100644 index 0000000..d2907af --- /dev/null +++ b/src/__snapshots__/constraints/default.test.ts.snap @@ -0,0 +1,193 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Default value constraints should process default constraints specified for parameters 1`] = ` +"
+ +# DefaultValueConstraints + +## Request + +
+ +
+ +
+ +POST {.openapi__method} +\`\`\` +http://localhost:8080/test +\`\`\` + + + +
+ +Generated server url + +
+ +
+ +### Query parameters + +#||| + **Name** +| + **Description** +|| + +|| + limit +| + **Type:** number<int32> + +Amount of search results to show
Default: \`10\` +|||# + +## Responses + +
+ +## 204 No Content + +
+ +### Body + +{% cut "application/json" %} + + +\`\`\`json +{} +\`\`\` + + +{% endcut %} + + +
+ +
+ + +
" +`; + +exports[`Default value constraints should process default constraints specified in schema objects 1`] = ` +"
+ +# DefaultValueConstraints + +## Request + +
+ +
+ +
+ +POST {.openapi__method} +\`\`\` +http://localhost:8080/test +\`\`\` + + + +
+ +Generated server url + +
+ +
+ +
+ +### Body + +{% cut "application/json" %} + + +\`\`\`json +{ + "role": "basic", + "name": "Anonymous", + "tags": [ + "blah" + ], + "age": 7 +} +\`\`\` + + +{% endcut %} + + +#||| + **Name** +| + **Description** +|| + +|| + age +| + **Type:** number + +Default: \`7\` +|| + +|| + name +| + **Type:** string + +Default: \`Anonymous\` +|| + +|| + role +| + **Type:** string + +Role for the user being created
Default: \`basic\`
Enum: \`basic\`, \`admin\` +|| + +|| + tags +| + **Type:** string[] + +Default: \`blah\` +|||# + +
+ +## Responses + +
+ +## 204 No Content + +
+ +### Body + +{% cut "application/json" %} + + +\`\`\`json +{} +\`\`\` + + +{% endcut %} + + +
+ +
+ + +
" +`; diff --git a/src/__snapshots__/length.test.ts.snap b/src/__snapshots__/constraints/length.test.ts.snap similarity index 91% rename from src/__snapshots__/length.test.ts.snap rename to src/__snapshots__/constraints/length.test.ts.snap index 46155de..0e80f86 100644 --- a/src/__snapshots__/length.test.ts.snap +++ b/src/__snapshots__/constraints/length.test.ts.snap @@ -73,8 +73,7 @@ Generated server url | **Type:** [Cat](#cat) -From response - +From response || || @@ -82,8 +81,7 @@ From response | **Type:** [Cat](#cat) -Cat class - +Cat class || || @@ -91,8 +89,7 @@ Cat class | **Type:** [Dog](#dog) -Dog class - +Dog class |||# @@ -120,10 +117,7 @@ Cat class || name | - **Type:** string - - - + **Type:** string |||# @@ -153,8 +147,7 @@ Dog class | **Type:** string -Pet name -
Max length: \`100\` +Pet name
Max length: \`100\` |||# diff --git a/src/__snapshots__/description.test.ts.snap b/src/__snapshots__/description.test.ts.snap index 4b80988..7848b8b 100644 --- a/src/__snapshots__/description.test.ts.snap +++ b/src/__snapshots__/description.test.ts.snap @@ -74,8 +74,7 @@ Generated server url | **Type:** [Cat](#cat) -From response - +From response || || @@ -83,8 +82,7 @@ From response | **Type:** [Cat](#cat) -Cat class - +Cat class || || @@ -92,8 +90,7 @@ Cat class | **Type:** [Dog](#dog) -Dog class - +Dog class || || @@ -101,8 +98,7 @@ Dog class | **Type:** object -Simple description - +Simple description |||# @@ -122,19 +118,13 @@ Cat class || foo | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# @@ -154,19 +144,13 @@ Dog class || bar | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# diff --git a/src/__snapshots__/examples/array.test.ts.snap b/src/__snapshots__/examples/array.test.ts.snap index 19da5f1..f95947f 100644 --- a/src/__snapshots__/examples/array.test.ts.snap +++ b/src/__snapshots__/examples/array.test.ts.snap @@ -59,11 +59,7 @@ Generated server url || a | - **Type:** object[] - - - - + **Type:** object[] |||# @@ -160,11 +156,7 @@ Generated server url || a | - **Type:** any[] - - - - + **Type:** any[] |||# @@ -258,11 +250,7 @@ Generated server url a | **Type:** (string -or integer)[] - - - - +or integer)[] |||# @@ -447,10 +435,7 @@ Generated server url || name | - **Type:** string - - - + **Type:** string |||# @@ -552,10 +537,7 @@ Generated server url || name | - **Type:** string - - - + **Type:** string |||# diff --git a/src/__snapshots__/examples/base.test.ts.snap b/src/__snapshots__/examples/base.test.ts.snap index 7055f34..fbdeab9 100644 --- a/src/__snapshots__/examples/base.test.ts.snap +++ b/src/__snapshots__/examples/base.test.ts.snap @@ -132,10 +132,7 @@ Generated server url || name | - **Type:** string - - - + **Type:** string |||# @@ -243,10 +240,7 @@ Generated server url || name | - **Type:** string - - - + **Type:** string |||# diff --git a/src/__snapshots__/hidden/objectProps.test.ts.snap b/src/__snapshots__/hidden/objectProps.test.ts.snap index 5703c2e..1bf53ca 100644 --- a/src/__snapshots__/hidden/objectProps.test.ts.snap +++ b/src/__snapshots__/hidden/objectProps.test.ts.snap @@ -57,8 +57,7 @@ Generated server url | **Type:** string -Morgan-Keenan luminosity class for this star - +Morgan-Keenan luminosity class for this star || || @@ -66,8 +65,7 @@ Morgan-Keenan luminosity class for this star | **Type:** string -Name of this star - +Name of this star |||# diff --git a/src/__snapshots__/objectPropertyOrder.test.ts.snap b/src/__snapshots__/objectPropertyOrder.test.ts.snap index 6217bab..9065ee4 100644 --- a/src/__snapshots__/objectPropertyOrder.test.ts.snap +++ b/src/__snapshots__/objectPropertyOrder.test.ts.snap @@ -59,8 +59,7 @@ Generated server url | **Type:** string<uuid> -Internal ID for this star - +Internal ID for this star || || @@ -68,8 +67,7 @@ Internal ID for this star | **Type:** string -Morgan-Keenan luminosity class for this star - +Morgan-Keenan luminosity class for this star || || @@ -77,8 +75,7 @@ Morgan-Keenan luminosity class for this star | **Type:** string -Name of this star - +Name of this star || || @@ -86,8 +83,7 @@ Name of this star | **Type:** string -CCDM catalogue designation for this star - +CCDM catalogue designation for this star |||# @@ -180,8 +176,7 @@ Generated server url | **Type:** string -CCDM catalogue designation for this star - +CCDM catalogue designation for this star || || @@ -189,8 +184,7 @@ CCDM catalogue designation for this star | **Type:** string<uuid> -Internal ID for this star - +Internal ID for this star || || @@ -198,8 +192,7 @@ Internal ID for this star | **Type:** string -Morgan-Keenan luminosity class for this star - +Morgan-Keenan luminosity class for this star || || @@ -207,8 +200,7 @@ Morgan-Keenan luminosity class for this star | **Type:** string -Name of this star - +Name of this star |||# diff --git a/src/__snapshots__/required.test.ts.snap b/src/__snapshots__/required.test.ts.snap index b15651f..57dd4f8 100644 --- a/src/__snapshots__/required.test.ts.snap +++ b/src/__snapshots__/required.test.ts.snap @@ -81,46 +81,31 @@ Generated server url || a* | - **Type:** number - - - + **Type:** number || || b* | - **Type:** number - - - + **Type:** number || || d* | - **Type:** number - - - + **Type:** number || || c | - **Type:** number - - - + **Type:** number || || e | - **Type:** number - - - + **Type:** number |||# @@ -160,19 +145,13 @@ Cat class || foo | - **Type:** string - - - + **Type:** string || || type | - **Type:** string - - - + **Type:** string |||# diff --git a/src/__tests__/constraints/default.test.ts b/src/__tests__/constraints/default.test.ts new file mode 100644 index 0000000..b4d625b --- /dev/null +++ b/src/__tests__/constraints/default.test.ts @@ -0,0 +1,68 @@ +import {DocumentBuilder, run} from '../__helpers__/run'; + +const mockDocumentName = 'DefaultValueConstraints'; + +describe('Default value constraints', () => { + it('should process default constraints specified for parameters', async () => { + const spec = new DocumentBuilder(mockDocumentName) + .parameter({ + in: 'query', + name: 'limit', + description: 'Amount of search results to show', + schema: { + type: 'number', + format: 'int32', + default: 10, + }, + }) + .response(204, {}) + .build(); + + const fs = await run(spec); + + const page = fs.match(mockDocumentName); + + expect(page).toMatchSnapshot(); + }); + + it('should process default constraints specified in schema objects', async () => { + const spec = new DocumentBuilder(mockDocumentName) + .component('CreateUserRequestDto', { + type: 'object', + properties: { + role: { + description: 'Role for the user being created', + type: 'string', + enum: ['basic', 'admin'], + default: 'basic', + }, + name: { + type: 'string', + default: 'Anonymous', + }, + tags: { + type: 'array', + items: { + type: 'string', + default: 'blah', + }, + }, + age: { + type: 'number', + default: 7, + }, + }, + }) + .request({ + schema: DocumentBuilder.ref('CreateUserRequestDto'), + }) + .response(204, {}) + .build(); + + const fs = await run(spec); + + const page = fs.match(mockDocumentName); + + expect(page).toMatchSnapshot(); + }); +}); diff --git a/src/__tests__/length.test.ts b/src/__tests__/constraints/length.test.ts similarity index 97% rename from src/__tests__/length.test.ts rename to src/__tests__/constraints/length.test.ts index 9c3d5ba..35aa564 100644 --- a/src/__tests__/length.test.ts +++ b/src/__tests__/constraints/length.test.ts @@ -1,4 +1,4 @@ -import {DocumentBuilder, run} from './__helpers__/run'; +import {DocumentBuilder, run} from '../__helpers__/run'; const name = 'length'; describe('length', () => { diff --git a/src/includer/traverse/description.ts b/src/includer/traverse/description.ts index c7653c1..c110d97 100644 --- a/src/includer/traverse/description.ts +++ b/src/includer/traverse/description.ts @@ -1,4 +1,3 @@ -import {EOL} from '../constants'; import {OpenJSONSchema} from '../models'; import {concatNewLine} from '../utils'; @@ -74,10 +73,8 @@ const fields: Fields = [ ]; function prepareComplexDescription(baseDescription: string, value: OpenJSONSchema): string { - const description = baseDescription + EOL; - return fields.reduce((acc, curr) => { - const field = typeof curr === 'function' ? curr(value) : curr; + const field = typeof curr === 'function' ? curr(value) : curr; //? if (typeof field === 'undefined' || !value[field.key]) { return acc; @@ -86,7 +83,7 @@ function prepareComplexDescription(baseDescription: string, value: OpenJSONSchem const {key, label, computed, notWrapValueIntoCode} = field; return concatConstraint(acc, computed || value[key], label + ':', notWrapValueIntoCode); - }, description); + }, baseDescription); } function concatConstraint( diff --git a/src/includer/traverse/types.ts b/src/includer/traverse/types.ts index 7ecb19f..694a7da 100644 --- a/src/includer/traverse/types.ts +++ b/src/includer/traverse/types.ts @@ -81,7 +81,7 @@ function extractRefFromType(type: JSONSchemaType): string | undefined { return type.ref; } -function collectRefs(type: JSONSchemaType): string[] | undefined { +function collectRefs(type: JSONSchemaType): string[] { const result: JSONSchemaType[] = []; if (isUnionType(type)) { @@ -90,7 +90,9 @@ function collectRefs(type: JSONSchemaType): string[] | undefined { result.push(type); } - return result.map(extractRefFromType).filter(Boolean) as string[] | undefined; + return result + .map(extractRefFromType) + .filter((maybeRef): maybeRef is string => typeof maybeRef !== 'undefined'); } function isUnionType(type: JSONSchemaType): type is JSONSchemaUnionType { diff --git a/src/includer/ui/endpoint.ts b/src/includer/ui/endpoint.ts index 0dc21fd..c2c0b46 100644 --- a/src/includer/ui/endpoint.ts +++ b/src/includer/ui/endpoint.ts @@ -223,7 +223,7 @@ function parameters(pagePrintedRefs: Set, params?: Parameters) { function parameterRow(param: Parameter): {cells: string[]; ref?: TableRef[]} { const row = prepareTableRowData(param.schema, param.name); let description = param.description ?? ''; - if (!row.ref && row.description.length) { + if (!row.ref?.length && row.description.length) { // if row.ref present, row.description will be printed in separate table description = concatNewLine(description, row.description); }