Skip to content

Commit

Permalink
Merge pull request #25 from diplodoc-platform/features_pack
Browse files Browse the repository at this point in the history
  • Loading branch information
v8tenko authored Nov 29, 2023
2 parents bcddb30 + 3783dff commit 06708fc
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 17 deletions.
231 changes: 231 additions & 0 deletions src/__snapshots__/examples/array.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`openapi project with examples renders example field 1`] = `
"<div class="openapi">
# example.array
## 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}
#### Body
{% cut "application/json" %}
\`\`\`json
[
{
"test": 1
},
{
"test": 2
}
]
\`\`\`
{% endcut %}
any[]
## Responses
<div class="openapi__response__code__200">
## 200 OK
Base 200 response
#### Body
{% cut "application/json" %}
\`\`\`json
{}
\`\`\`
{% endcut %}
</div>
<!-- markdownlint-disable-file -->
</div>"
`;

exports[`openapi project with examples renders infered example 1`] = `
"<div class="openapi">
# example.array
## 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}
#### Body
{% cut "application/json" %}
\`\`\`json
[
{
"name": "string"
}
]
\`\`\`
{% endcut %}
[Cat](#cat)[]
### Cat
#||| **Name** | **Type** | **Description** ||
|| name | string | |||#
## Responses
<div class="openapi__response__code__200">
## 200 OK
Base 200 response
#### Body
{% cut "application/json" %}
\`\`\`json
{}
\`\`\`
{% endcut %}
</div>
<!-- markdownlint-disable-file -->
</div>"
`;

exports[`openapi project with examples renders nested arrays exmaple 1`] = `
"<div class="openapi">
# example.array
## 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}
#### Body
{% cut "application/json" %}
\`\`\`json
[
[
{
"name": "string"
}
]
]
\`\`\`
{% endcut %}
[Cat](#cat)[][]
### Cat
#||| **Name** | **Type** | **Description** ||
|| name | string | |||#
## Responses
<div class="openapi__response__code__200">
## 200 OK
Base 200 response
#### Body
{% cut "application/json" %}
\`\`\`json
{}
\`\`\`
{% endcut %}
</div>
<!-- markdownlint-disable-file -->
</div>"
`;
File renamed without changes.
100 changes: 100 additions & 0 deletions src/__tests__/examples/array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {DocumentBuilder, run} from '../__helpers__/run';

const name = 'example.array';
describe('openapi project with examples', () => {
it('renders example field', async () => {
const spec = new DocumentBuilder(name)
.request({
schema: {
example: [
{
test: 1,
},
{
test: 2,
},
],
type: 'array',
items: {},
},
})
.response(200, {
description: 'Base 200 response',
schema: {
type: 'object',
},
})
.build();

const fs = await run(spec);

const page = fs.match(name);

expect(page).toMatchSnapshot();
});

it('renders infered example', async () => {
const spec = new DocumentBuilder(name)
.request({
schema: {
type: 'array',
items: DocumentBuilder.ref('Cat'),
},
})
.response(200, {
description: 'Base 200 response',
schema: {
type: 'object',
},
})
.component('Cat', {
type: 'object',
properties: {
name: {
type: 'string',
},
},
})
.build();

const fs = await run(spec);

const page = fs.match(name);

expect(page).toMatchSnapshot();
});

it('renders nested arrays exmaple', async () => {
const spec = new DocumentBuilder(name)
.request({
schema: {
type: 'array',
items: {
type: 'array',
items: DocumentBuilder.ref('Cat'),
},
},
})
.response(200, {
description: 'Base 200 response',
schema: {
type: 'object',
},
})
.component('Cat', {
type: 'object',
properties: {
name: {
type: 'string',
},
},
})
.build();

const fs = await run(spec);

const page = fs.match(name);

expect(page).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {DocumentBuilder, run} from './__helpers__/run';
import {DocumentBuilder, run} from '../__helpers__/run';

const name = 'example';
describe('openapi project with examples', () => {
Expand Down
21 changes: 14 additions & 7 deletions src/includer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,20 @@ async function generateToc(params: GenerateTocParams): Promise<void> {
items: [],
};

section.items = endpointsOfTag.map((endpoint) => handleEndpointRender(endpoint, id));

const custom = ArgvService.tag(tag.name);
const customId = custom?.alias || id;

section.items = endpointsOfTag.map((endpoint) => handleEndpointRender(endpoint, customId));

const customLeadingPageName = custom?.name || leadingPageName;

if (!custom?.hidden) {
addLeadingPage(section, leadingPageMode, customLeadingPageName, join(id, 'index.md'));
addLeadingPage(
section,
leadingPageMode,
customLeadingPageName,
join(customId, 'index.md'),
);
}

toc.items.push(section);
Expand Down Expand Up @@ -270,12 +276,13 @@ async function generateContent(params: GenerateContentParams): Promise<void> {
spec.tags.forEach((tag, id) => {
const {endpoints} = tag;

const custom = ArgvService.tag(tag.name);
const customId = custom?.alias || id;

endpoints.forEach((endpoint) => {
results.push(handleEndpointIncluder(endpoint, join(writePath, id), sandbox));
results.push(handleEndpointIncluder(endpoint, join(writePath, customId), sandbox));
});

const custom = ArgvService.tag(tag.name);

if (custom?.hidden) {
return;
}
Expand All @@ -285,7 +292,7 @@ async function generateContent(params: GenerateContentParams): Promise<void> {
: generators.section(tag);

results.push({
path: join(writePath, id, 'index.md'),
path: join(writePath, customId, 'index.md'),
content,
});
});
Expand Down
1 change: 1 addition & 0 deletions src/includer/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ export type CustomTag = {
hidden?: boolean;
name?: string;
path?: string;
alias?: string;
};

export type OpenApiIncluderParams = {
Expand Down
Loading

0 comments on commit 06708fc

Please sign in to comment.