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

Commit 4945b42

Browse files
committed
No longer dereference schema
1 parent 76fbd10 commit 4945b42

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"test:project": "cd test/project && yarn sls openapi generate",
3131
"test:prepare": "scripts/prepareTests.bash",
3232
"build:link": "yarn build && cd build && yarn link",
33-
"build:watch": "yarn build && tsc --watch",
33+
"build:watch": "yarn build && yarn tsc --watch",
3434
"build": "scripts/build.bash"
3535
},
3636
"devDependencies": {

src/DefinitionGenerator.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { dereference } from '@jdw/jst';
21
// tslint:disable-next-line no-submodule-imports
32
import { validateSync as openApiValidatorSync } from 'swagger2openapi/validate';
43
import * as uuid from 'uuid';
54
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';
77

88
export class DefinitionGenerator {
99
// The OpenAPI version we currently validate against
@@ -49,15 +49,40 @@ export class DefinitionGenerator {
4949
continue;
5050
}
5151

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))
5562
}
5663
}
5764

5865
return this;
5966
}
6067

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+
6186
public validate (): { valid: boolean, context: string[], warnings: any[], error?: any[] } {
6287
const payload: any = {};
6388

src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ export function isIterable (obj) {
1010

1111
return typeof obj[Symbol.iterator] === 'function';
1212
}
13+
14+
export function omit<T extends Object> (obj: T, keys: string[]): T {
15+
const cloned = clone(obj);
16+
17+
for(const key of keys) {
18+
delete cloned[key];
19+
}
20+
21+
return cloned;
22+
}

0 commit comments

Comments
 (0)