Skip to content

Commit

Permalink
support length limits
Browse files Browse the repository at this point in the history
  • Loading branch information
mickxolotl committed Jan 23, 2024
1 parent c483dbe commit 4332d2b
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 0 deletions.
92 changes: 92 additions & 0 deletions src/__snapshots__/length.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`length renders correct length limits 1`] = `
"<div class="openapi">
# length
## Request
<div class="openapi__request__wrapper">
<div class="openapi__request" style="--method: var(--dc-openapi-methods-post)">
POST {.openapi__method}
\`\`\`
http://localhost:8080/test
\`\`\`
</div>
</div>
Generated server url{.openapi__request__description}
## Responses
<div class="openapi__response__code__200">
## 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 | <span style="color:gray;">Min length</span>: \`3\` |||#
### Dog
Dog class
#||| **Name** | **Type** | **Description** ||
|| name | string | Pet name<br><span style="color:gray;">Max length</span>: \`100\` ||
|| bar | string | <span style="color:gray;">Min length</span>: \`1\`<br><span style="color:gray;">Max length</span>: \`99\` |||#
</div>
<!-- markdownlint-disable-file -->
</div>"
`;
63 changes: 63 additions & 0 deletions src/__tests__/length.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
13 changes: 13 additions & 0 deletions src/includer/traverse/tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,19 @@ function prepareComplexDescription(baseDescription: string, value: OpenJSONSchem
);
}

if (typeof value.minLength !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Min length</span>: \`${value.minLength}\``,
);
}
if (typeof value.maxLength !== 'undefined') {
description = concatNewLine(
description,
`<span style="color:gray;">Max length</span>: \`${value.maxLength}\``,
);
}

return description;
}

Expand Down

0 comments on commit 4332d2b

Please sign in to comment.