|
1 |
| -import { dereference } from '@jdw/jst'; |
2 | 1 | // tslint:disable-next-line no-submodule-imports
|
3 | 2 | import { validateSync as openApiValidatorSync } from 'swagger2openapi/validate';
|
4 | 3 | import * as uuid from 'uuid';
|
5 | 4 | import { IDefinition, IDefinitionConfig, IOperation, IParameterConfig, IServerlessFunctionConfig } from './types';
|
6 |
| -import { clone, isIterable, merge } from './utils'; |
| 5 | +import { clone, isIterable, merge, omit } from './utils'; |
| 6 | +import { JSONSchema7 } from 'json-schema'; |
7 | 7 |
|
8 | 8 | export class DefinitionGenerator {
|
9 | 9 | // The OpenAPI version we currently validate against
|
@@ -49,15 +49,40 @@ export class DefinitionGenerator {
|
49 | 49 | continue;
|
50 | 50 | }
|
51 | 51 |
|
52 |
| - this.definition.components.schemas[model.name] = this.cleanSchema( |
53 |
| - dereference(model.schema), |
54 |
| - ); |
| 52 | + for (const definitionName of Object.keys(model.schema.definitions || {})) { |
| 53 | + const definition = model.schema.definitions[definitionName]; |
| 54 | + if(typeof definition !== 'boolean'){ |
| 55 | + this.definition.components.schemas[definitionName] = this.cleanSchema(this.updateReferences(definition)) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + const schemaWithoutDefinitions = omit(model.schema, ['definitions']); |
| 60 | + |
| 61 | + this.definition.components.schemas[model.name] = this.cleanSchema(this.updateReferences(schemaWithoutDefinitions)) |
55 | 62 | }
|
56 | 63 | }
|
57 | 64 |
|
58 | 65 | return this;
|
59 | 66 | }
|
60 | 67 |
|
| 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 | + |
61 | 86 | public validate (): { valid: boolean, context: string[], warnings: any[], error?: any[] } {
|
62 | 87 | const payload: any = {};
|
63 | 88 |
|
|
0 commit comments