Skip to content

Commit

Permalink
refactor: pull change how schema is stored in the operation class (#301)
Browse files Browse the repository at this point in the history
* feat: pulling in schema sample creation code from swaggerui

* chore: removing dead code

* feat: addition of a new getResponseExamples method on the operation class

* feat: adding a new `getRequestBodyExamples` method on the operation

* chore: removing memoizee

* fix: stop generating examples for xml schemas

* refactor: operation schema data no longer fills the `this` scope of the operation

* feat: adding a new `getParametersAsJsonSchema` on the operation
  • Loading branch information
erunion authored Nov 10, 2020
1 parent 71640d4 commit 83ddd52
Show file tree
Hide file tree
Showing 13 changed files with 496 additions and 186 deletions.
4 changes: 3 additions & 1 deletion __tests__/tooling/__snapshots__/oas.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ Operation {
"oas": Oas {
"user": Object {},
},
"parameters": Array [],
"path": "/unknown",
"requestBodyExamples": undefined,
"responseExamples": undefined,
"schema": Object {
"parameters": Array [],
},
}
`;
245 changes: 245 additions & 0 deletions __tests__/tooling/__snapshots__/operation.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,250 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`#getParametersAsJsonSchema() should return json schema 1`] = `
Array [
Object {
"label": "Body Params",
"schema": Object {
"$ref": "#/components/schemas/Pet",
"components": Object {
"requestBodies": Object {
"Pet": Object {
"content": Object {
"application/json": Object {
"schema": Object {
"$ref": "#/components/schemas/Pet",
},
},
"application/xml": Object {
"schema": Object {
"$ref": "#/components/schemas/Pet",
},
},
},
"description": "Pet object that needs to be added to the store",
"required": true,
},
"UserArray": Object {
"content": Object {
"application/json": Object {
"schema": Object {
"items": Object {
"$ref": "#/components/schemas/User",
},
"type": "array",
},
},
},
"description": "List of user object",
"required": true,
},
},
"schemas": Object {
"ApiResponse": Object {
"properties": Object {
"code": Object {
"format": "int32",
"type": "integer",
},
"message": Object {
"type": "string",
},
"type": Object {
"type": "string",
},
},
"type": "object",
},
"Category": Object {
"properties": Object {
"id": Object {
"format": "int64",
"type": "integer",
},
"name": Object {
"type": "string",
},
},
"type": "object",
"xml": Object {
"name": "Category",
"type": "string",
},
},
"Order": Object {
"properties": Object {
"complete": Object {
"default": false,
"type": "boolean",
},
"id": Object {
"format": "int64",
"type": "integer",
},
"petId": Object {
"format": "int64",
"type": "integer",
},
"quantity": Object {
"format": "int32",
"type": "integer",
},
"shipDate": Object {
"format": "date-time",
"type": "string",
},
"status": Object {
"description": "Order Status",
"enum": Array [
"placed",
"approved",
"delivered",
],
"type": "string",
},
},
"type": "object",
"xml": Object {
"name": "Order",
"type": "string",
},
},
"Pet": Object {
"properties": Object {
"category": Object {
"$ref": "#/components/schemas/Category",
},
"id": Object {
"format": "int64",
"readOnly": true,
"type": "integer",
},
"name": Object {
"example": "doggie",
"type": "string",
},
"photoUrls": Object {
"items": Object {
"type": "string",
},
"type": "array",
"xml": Object {
"name": "photoUrl",
"type": "string",
"wrapped": true,
},
},
"status": Object {
"description": "pet status in the store",
"enum": Array [
"available",
"pending",
"sold",
],
"type": "string",
},
"tags": Object {
"items": Object {
"$ref": "#/components/schemas/Tag",
},
"type": "array",
"xml": Object {
"name": "tag",
"type": "string",
"wrapped": true,
},
},
},
"required": Array [
"name",
"photoUrls",
],
"type": "object",
"xml": Object {
"name": "Pet",
"type": "string",
},
},
"Tag": Object {
"properties": Object {
"id": Object {
"format": "int64",
"type": "integer",
},
"name": Object {
"type": "string",
},
},
"type": "object",
"xml": Object {
"name": "Tag",
"type": "string",
},
},
"User": Object {
"properties": Object {
"email": Object {
"type": "string",
},
"firstName": Object {
"type": "string",
},
"id": Object {
"format": "int64",
"type": "integer",
},
"lastName": Object {
"type": "string",
},
"password": Object {
"type": "string",
},
"phone": Object {
"type": "string",
},
"userStatus": Object {
"description": "User Status",
"format": "int32",
"type": "integer",
},
"username": Object {
"type": "string",
},
},
"type": "object",
"xml": Object {
"name": "User",
"type": "string",
},
},
},
"securitySchemes": Object {
"api_key": Object {
"in": "header",
"name": "api_key",
"type": "apiKey",
},
"petstore_auth": Object {
"_key": "petstore_auth",
"flows": Object {
"implicit": Object {
"authorizationUrl": "http://petstore.swagger.io/oauth/dialog",
"scopes": Object {
"read:pets": "read your pets",
"write:pets": "modify pets in your account",
},
},
},
"type": "oauth2",
},
},
},
},
"type": "body",
},
]
`;

exports[`#prepareSecurity() should work for petstore 1`] = `
Object {
"OAuth2": Array [
Expand Down
2 changes: 1 addition & 1 deletion __tests__/tooling/lib/find-schema-definition.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const petstore = require('@readme/oas-examples/3.0/json/petstore');
const petstore = require('@readme/oas-examples/3.0/json/petstore.json');
const findSchemaDefinition = require('../../../tooling/lib/find-schema-definition');

test('should return a definition for a given ref', () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/tooling/lib/flatten-schema.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const flattenSchema = require('../../../tooling/lib/flatten-schema');

const petstore = require('@readme/oas-examples/3.0/json/petstore');
const petstoreExpanded = require('@readme/oas-examples/3.0/json/petstore-expanded');
const petstore = require('@readme/oas-examples/3.0/json/petstore.json');
const petstoreExpanded = require('@readme/oas-examples/3.0/json/petstore-expanded.json');

test('should flatten schema to an array', async () => {
const schema = {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/tooling/lib/get-path.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const getPath = require('../../../tooling/lib/get-path');
const Oas = require('../../../tooling');

const petstore = require('@readme/oas-examples/3.0/json/petstore');
const petstore = require('@readme/oas-examples/3.0/json/petstore.json');

const swagger = new Oas(petstore);

Expand Down
41 changes: 41 additions & 0 deletions __tests__/tooling/lib/matches-mimetype.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const matchesMimeType = require('../../../tooling/lib/matches-mimetype');

describe('#formUrlEncoded', () => {
it('should recognize `application/x-www-form-urlencoded`', () => {
expect(matchesMimeType.formUrlEncoded('application/x-www-form-urlencoded')).toBe(true);
});
});

describe('#json', () => {
it.each([
['application/json'],
['application/x-json'],
['text/json'],
['text/x-json'],
['application/vnd.github.v3.star+json'],
])('should recognize `%s`', contentType => {
expect(matchesMimeType.json(contentType)).toBe(true);
});
});

describe('#multipart', () => {
it.each([['multipart/mixed'], ['multipart/related'], ['multipart/form-data'], ['multipart/alternative']])(
'should recognize `%s`',
contentType => {
expect(matchesMimeType.multipart(contentType)).toBe(true);
}
);
});

describe('#xml', () => {
it.each([
['application/xml'],
['application/xml-external-parsed-entity'],
['application/xml-dtd'],
['text/xml'],
['text/xml-external-parsed-entity'],
['application/vnd.github.v3.star+xml'],
])('should recognize `%s`', contentType => {
expect(matchesMimeType.xml(contentType)).toBe(true);
});
});
Loading

0 comments on commit 83ddd52

Please sign in to comment.