Skip to content

Commit

Permalink
feat: add test and improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk committed May 26, 2021
1 parent 83ed6f3 commit 6acdba5
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 43 deletions.
53 changes: 47 additions & 6 deletions demo/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
openapi: 3.0.0
openapi: 3.1.0
servers:
- url: //petstore.swagger.io/v2
description: Default server
Expand Down Expand Up @@ -42,6 +42,7 @@ info:
version: 1.0.0
title: Swagger Petstore
summary: My lovely API
termsOfService: 'http://swagger.io/terms/'
contact:
name: API Support
Expand All @@ -53,6 +54,7 @@ info:
license:
name: Apache 2.0
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
identifier: Apache 2.0
externalDocs:
description: Find out how to create Github repo for your OpenAPI spec.
url: 'https://github.com/Rebilly/generator-openapi-repo'
Expand Down Expand Up @@ -893,6 +895,38 @@ paths:
default:
description: successful operation
components:
pathItems:
catsWebhook:
put:
summary: Get a cat details after update
description: Get a cat details after update
operationId: updatedCat
tags:
- pet
requestBody:
description: Information about cat in the system
content:
multipart/form-data:
schema:
$ref: "#/components/schemas/Cat"
responses:
'200':
description: update Cat details
post:
summary: Create new cat
description: Info about new cat
operationId: createdCat
tags:
- pet
requestBody:
description: Information about cat in the system
content:
multipart/form-data:
schema:
$ref: "#/components/schemas/Cat"
responses:
'200':
description: create Cat details
schemas:
ApiResponse:
type: object
Expand Down Expand Up @@ -1040,7 +1074,8 @@ components:
example: Guru
photoUrls:
description: The list of URL to a cute photos featuring pet
type: array
type: [string, integer, 'null', array]
minItems: 1
maxItems: 20
xml:
name: photoUrl
Expand All @@ -1054,7 +1089,8 @@ components:
tags:
description: Tags attached to the pet
type: array
minItems: 1
exclusiveMaximum: 100
exclusiveMinimum: 0
xml:
name: tag
wrapped: true
Expand All @@ -1067,6 +1103,7 @@ components:
- available
- pending
- sold
default: pending
petType:
description: Type of a pet
type: string
Expand Down Expand Up @@ -1187,13 +1224,13 @@ components:
shipDate: '2018-10-19T16:46:45Z'
status: placed
complete: false
x-webhooks:
webhooks:
newPet:
post:
summary: New pet
description: Information about a new pet in the systems
operationId: newPet
tags:
tags:
- pet
requestBody:
content:
Expand All @@ -1202,4 +1239,8 @@ x-webhooks:
$ref: "#/components/schemas/Pet"
responses:
"200":
description: Return a 200 status to indicate that the data was received successfully
description: Return a 200 status to indicate that the data was received successfully
myWebhook:
$ref: '#/components/pathItems/catsWebhook'
description: Overriding description
summary: Overriding summary
2 changes: 1 addition & 1 deletion e2e/integration/menu.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('Menu', () => {
it('should have valid items count', () => {
cy.get('.menu-content')
.find('li')
.should('have.length', 34);
.should('have.length', 36);
});

it('should sync active menu items while scroll', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/Fields/FieldDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class FieldDetails extends React.PureComponent<FieldProps, { patternShown
<ExternalDocumentation externalDocs={schema.externalDocs} compact={true} />
)}
{(renderDiscriminatorSwitch && renderDiscriminatorSwitch(this.props)) || null}
{field.const && (<FieldDetail label={'Value:'} value={field.const}/>) || null}
{field.const && (<FieldDetail label={l('const') + ':'} value={field.const}/>) || null}
</div>
);
}
Expand Down
10 changes: 4 additions & 6 deletions src/services/MenuBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Referenced,
OpenAPIServer,
OpenAPIPaths,
OpenAPIPath,
} from '../types';
import {
isOperationName,
Expand Down Expand Up @@ -235,12 +234,11 @@ export class MenuBuilder {
for (const pathName of Object.keys(paths)) {
const path = paths[pathName];
const operations = Object.keys(path).filter(isOperationName);
for (let operationName of operations) {
let operationInfo = path[operationName];
for (const operationName of operations) {
const operationInfo = path[operationName];
if (path.$ref) {
const resolvedPath = parser.deref<OpenAPIPath>(path || {})
operationName = Object.keys(resolvedPath)[0]
operationInfo = resolvedPath[operationName]
const resolvedPaths = parser.deref<OpenAPIPaths>(path as OpenAPIPaths);
getTags(parser, { [operationName]: resolvedPaths }, isWebhook);
}
let operationTags = operationInfo?.tags;

Expand Down
66 changes: 66 additions & 0 deletions src/services/__tests__/fixtures/3.1/pathItems.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"openapi": "3.1.0",
"info": {
"version": "1.0.0",
"title": "Swagger Petstore"
},
"webhooks": {
"myWebhook": {
"$ref": "#/components/pathItems/catsWebhook",
"description": "Overriding description",
"summary": "Overriding summary"
}
},
"components": {
"pathItems": {
"catsWebhook": {
"put": {
"summary": "Get a cat details after update",
"description": "Get a cat details after update",
"operationId": "updatedCat",
"tags": [
"pet"
],
"requestBody": {
"description": "Information about cat in the system",
"content": {
"multipart/form-data": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"responses": {
"200": {
"description": "update Cat details"
}
}
},
"post": {
"summary": "Create new cat",
"description": "Info about new cat",
"operationId": "createdCat",
"tags": [
"pet"
],
"requestBody": {
"description": "Information about cat in the system",
"content": {
"multipart/form-data": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"responses": {
"200": {
"description": "create Cat details"
}
}
}
}
}
}
}
16 changes: 16 additions & 0 deletions src/services/__tests__/models/ApiInfo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,21 @@ describe('Models', () => {
const info = new ApiInfoModel(parser);
expect(info.summary).toEqual('Test summary\nsome text\n## Heading\n test');
});

test('should correctly populate license identifier', () => {
parser.spec = {
openapi: '3.1.0',
info: {
license: {
name: 'MIT',
identifier: 'MIT',
url: 'https://opensource.org/licenses/MIT'
}
},
} as any;

const { license = { identifier: null } } = new ApiInfoModel(parser);
expect(license.identifier).toEqual('MIT');
});
});
});
25 changes: 25 additions & 0 deletions src/services/__tests__/models/MenuBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { MenuBuilder } from '../../MenuBuilder';
import { OpenAPIParser } from '../../OpenAPIParser';

import { RedocNormalizedOptions } from '../../RedocNormalizedOptions';

const opts = new RedocNormalizedOptions({});

describe('Models', () => {
describe('MenuBuilder', () => {
let parser;

test('discriminator with one field', () => {
const spec = require('../fixtures/3.1/pathItems.json');
parser = new OpenAPIParser(spec, undefined, opts);
const contentItems = MenuBuilder.buildStructure(parser, opts);
expect(contentItems).toHaveLength(2);
expect(contentItems[0].items).toHaveLength(2);
expect(contentItems[0].id).toEqual('tag/pet');
expect(contentItems[0].name).toEqual('pet');
expect(contentItems[0].type).toEqual('tag');

});
});
});
10 changes: 6 additions & 4 deletions src/services/models/Webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ export class WebhookModel {
) {
const webhooks = parser.deref<OpenAPIPath>(infoOrRef || {});
parser.exitRef(infoOrRef);
this.initWebhooks(parser, webhooks, options);
}

initWebhooks(parser: OpenAPIParser, webhooks: OpenAPIPath, options: RedocNormalizedOptions) {
for (const webhookName of Object.keys(webhooks)) {
const webhook = webhooks[webhookName];
const operations = Object.keys(webhook).filter(isOperationName);
for (let operationName of operations) {
let operationInfo = webhook[operationName];
for (const operationName of operations) {
const operationInfo = webhook[operationName];
if (webhook.$ref) {
const resolvedWebhook = parser.deref<OpenAPIPath>(webhook || {});
operationName = Object.keys(resolvedWebhook)[0];
operationInfo = resolvedWebhook[operationName];
this.initWebhooks(parser, { [operationName]: resolvedWebhook }, options);
}

if (!operationInfo) continue;
Expand Down
Loading

0 comments on commit 6acdba5

Please sign in to comment.