Skip to content
This repository was archived by the owner on Dec 10, 2021. It is now read-only.

Support recursive schema #1

Merged
merged 7 commits into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "serverless-openapi-documentation",
"version": "0.4.0",
"version": "1.0.0",
"description": "Serverless 1.0 plugin to generate OpenAPI V3 documentation from serverless configuration",
"main": "index.js",
"repository": {
Expand Down Expand Up @@ -39,6 +39,7 @@
"@types/fs-extra": "^4.0.0",
"@types/jest": "^20.0.2",
"@types/js-yaml": "^3.5.31",
"@types/json-schema": "^7.0.3",
"@types/node": "^8.0.7",
"@types/uuid": "^3.0.0",
"changelog-verify": "^1.0.4",
Expand All @@ -52,7 +53,6 @@
"version-changelog": "^2.1.0"
},
"dependencies": {
"@jdw/jst": "^2.0.0-beta.9",
"bluebird": "^3.5.0",
"chalk": "^2.0.1",
"fs-extra": "^4.0.1",
Expand Down
40 changes: 35 additions & 5 deletions src/DefinitionGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { dereference } from '@jdw/jst';
import { JSONSchema7 } from 'json-schema';
// tslint:disable-next-line no-submodule-imports
import { validateSync as openApiValidatorSync } from 'swagger2openapi/validate';
import * as uuid from 'uuid';

import { IDefinition, IDefinitionConfig, IOperation, IParameterConfig, IServerlessFunctionConfig } from './types';
import { clone, isIterable, merge } from './utils';
import { clone, isIterable, merge, omit } from './utils';

export class DefinitionGenerator {
// The OpenAPI version we currently validate against
Expand Down Expand Up @@ -49,9 +50,16 @@ export class DefinitionGenerator {
continue;
}

this.definition.components.schemas[model.name] = this.cleanSchema(
dereference(model.schema),
);
for (const definitionName of Object.keys(model.schema.definitions || {})) {
const definition = model.schema.definitions[definitionName];
if (typeof definition !== 'boolean') {
this.definition.components.schemas[definitionName] = this.cleanSchema(this.updateReferences(definition));
}
}

const schemaWithoutDefinitions = omit(model.schema, ['definitions']);

this.definition.components.schemas[model.name] = this.cleanSchema(this.updateReferences(schemaWithoutDefinitions));
}
}

Expand Down Expand Up @@ -116,6 +124,28 @@ export class DefinitionGenerator {
return cleanedSchema;
}

/**
* Walks through the schema object recursively and updates references to point to openapi's components
* @param schema JSON Schema Object
*/
private updateReferences (schema: JSONSchema7): JSONSchema7 {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this var have a version number in it? Shouldn't it just be a generic word like "schema"? As the schema version is not relevant to this function.

const cloned = clone(schema) as JSONSchema7;

if (cloned.$ref) {
cloned.$ref = cloned.$ref.replace('#/definitions', '#/components/schemas');
} else {
for (const key of Object.getOwnPropertyNames(cloned)) {
const value = cloned[key];

if (typeof value === 'object') {
cloned[key] = this.updateReferences(value);
}
}
}

return cloned;
}

/**
* Generate Operation objects from the Serverless Config.
*
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { JSONSchema7 } from 'json-schema';

export interface IModels {
name: string;
description: string;
contentType: string;
schema: object | any[];
schema: JSONSchema7;
examples: any[];
example: object;
}
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ export function isIterable (obj) {

return typeof obj[Symbol.iterator] === 'function';
}

export function omit<T extends object> (obj: T, keys: string[]): T {
const cloned = clone(obj);

for (const key of keys) {
delete cloned[key];
}

return cloned;
}
31 changes: 31 additions & 0 deletions test/project/models/RecursiveResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"folder": {
"type": "object",
"title": "Folder Schema",
"properties": {
"id": {
"type": "string",
"example": "3ba1c69c-3c81-4d6d-b304-873d898b2e3c",
"format": "uuid"
},
"folders": {
"type": "array",
"items": {
"$ref": "#/definitions/folder"
}
}
},
"additionalProperties": false,
"required": ["id", "name", "folders", "checklists"]
}
},
"type": "object",
"title": "Folders Response Schema",
"properties": {
"data": {
"$ref": "#/definitions/folder"
}
}
}
Loading