|
| 1 | +import { JSONSchema7 } from 'json-schema'; |
1 | 2 | // tslint:disable-next-line no-submodule-imports
|
2 | 3 | import { validateSync as openApiValidatorSync } from 'swagger2openapi/validate';
|
3 | 4 | import * as uuid from 'uuid';
|
| 5 | + |
4 | 6 | import { IDefinition, IDefinitionConfig, IOperation, IParameterConfig, IServerlessFunctionConfig } from './types';
|
5 | 7 | import { clone, isIterable, merge, omit } from './utils';
|
6 |
| -import { JSONSchema7 } from 'json-schema'; |
7 | 8 |
|
8 | 9 | export class DefinitionGenerator {
|
9 | 10 | // The OpenAPI version we currently validate against
|
@@ -51,38 +52,20 @@ export class DefinitionGenerator {
|
51 | 52 |
|
52 | 53 | for (const definitionName of Object.keys(model.schema.definitions || {})) {
|
53 | 54 | const definition = model.schema.definitions[definitionName];
|
54 |
| - if(typeof definition !== 'boolean'){ |
55 |
| - this.definition.components.schemas[definitionName] = this.cleanSchema(this.updateReferences(definition)) |
| 55 | + if (typeof definition !== 'boolean') { |
| 56 | + this.definition.components.schemas[definitionName] = this.cleanSchema(this.updateReferences(definition)); |
56 | 57 | }
|
57 | 58 | }
|
58 | 59 |
|
59 | 60 | const schemaWithoutDefinitions = omit(model.schema, ['definitions']);
|
60 | 61 |
|
61 |
| - this.definition.components.schemas[model.name] = this.cleanSchema(this.updateReferences(schemaWithoutDefinitions)) |
| 62 | + this.definition.components.schemas[model.name] = this.cleanSchema(this.updateReferences(schemaWithoutDefinitions)); |
62 | 63 | }
|
63 | 64 | }
|
64 | 65 |
|
65 | 66 | return this;
|
66 | 67 | }
|
67 | 68 |
|
68 |
| - private updateReferences(schema: JSONSchema7): JSONSchema7 { |
69 |
| - const cloned = clone(schema) as JSONSchema7; |
70 |
| - |
71 |
| - if(cloned.$ref) { |
72 |
| - cloned.$ref = cloned.$ref.replace('#/definitions', '#/components/schemas'); |
73 |
| - } else { |
74 |
| - for(const key of Object.getOwnPropertyNames(cloned)) { |
75 |
| - const value = cloned[key]; |
76 |
| - |
77 |
| - if (typeof value === 'object') { |
78 |
| - cloned[key] = this.updateReferences(value); |
79 |
| - } |
80 |
| - } |
81 |
| - } |
82 |
| - |
83 |
| - return cloned; |
84 |
| - } |
85 |
| - |
86 | 69 | public validate (): { valid: boolean, context: string[], warnings: any[], error?: any[] } {
|
87 | 70 | const payload: any = {};
|
88 | 71 |
|
@@ -141,6 +124,28 @@ export class DefinitionGenerator {
|
141 | 124 | return cleanedSchema;
|
142 | 125 | }
|
143 | 126 |
|
| 127 | + /** |
| 128 | + * Walks through the schema object recursively and updates references to point to openapi's components |
| 129 | + * @param schema JSON Schema Object |
| 130 | + */ |
| 131 | + private updateReferences (schema: JSONSchema7): JSONSchema7 { |
| 132 | + const cloned = clone(schema) as JSONSchema7; |
| 133 | + |
| 134 | + if (cloned.$ref) { |
| 135 | + cloned.$ref = cloned.$ref.replace('#/definitions', '#/components/schemas'); |
| 136 | + } else { |
| 137 | + for (const key of Object.getOwnPropertyNames(cloned)) { |
| 138 | + const value = cloned[key]; |
| 139 | + |
| 140 | + if (typeof value === 'object') { |
| 141 | + cloned[key] = this.updateReferences(value); |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return cloned; |
| 147 | + } |
| 148 | + |
144 | 149 | /**
|
145 | 150 | * Generate Operation objects from the Serverless Config.
|
146 | 151 | *
|
|
0 commit comments