Skip to content

Commit 989b3e9

Browse files
authored
Merge pull request #244 from JaredCE/0.0.113
0.0.113
2 parents c6db981 + 76711ef commit 989b3e9

9 files changed

+74
-60
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "serverless-openapi-documenter",
3-
"version": "0.0.112",
3+
"version": "0.0.113",
44
"description": "Generate OpenAPI v3 documentation and Postman Collections from your Serverless Config",
55
"main": "index.js",
66
"keywords": [

src/definitionGenerator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ class DefinitionGenerator {
624624
continue;
625625
}
626626

627-
const obj = JSON.parse(JSON.stringify(this.DEFAULT_CORS_HEADERS[key]));
627+
const obj = structuredClone(this.DEFAULT_CORS_HEADERS[key]);
628628

629629
if (key === "Access-Control-Allow-Origin") {
630630
if (

src/openAPIGenerator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class OpenAPIGenerator {
241241
};
242242

243243
PostmanGenerator.convert(
244-
{ type: "json", data: JSON.parse(JSON.stringify(openAPI)) },
244+
{ type: "json", data: structuredClone(openAPI) },
245245
{},
246246
postmanGeneration
247247
);

src/schemaHandler.js

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,15 @@ class SchemaHandler {
7171
const modelName = model.name;
7272
const modelSchema = model.schema;
7373

74-
this.logger.verbose(`dereferencing model: ${model.name}`);
75-
const dereferencedSchema = await this.__dereferenceSchema(
76-
modelSchema
74+
const convertedSchemas = await this.__dereferenceAndConvert(
75+
modelSchema,
76+
modelName,
77+
model
7778
).catch((err) => {
78-
if (err.errors) {
79-
for (const error of err?.errors) {
80-
this.__HTTPError(error, model);
81-
}
82-
} else {
83-
this.__HTTPError(err, model);
84-
}
85-
return modelSchema;
79+
if (err instanceof Error) throw err;
80+
else return err;
8681
});
8782

88-
this.logger.verbose(`convering model: ${model.name}`);
89-
const convertedSchemas = SchemaConvertor.convert(
90-
dereferencedSchema,
91-
modelName
92-
);
93-
9483
if (
9584
typeof convertedSchemas.schemas === "object" &&
9685
!Array.isArray(convertedSchemas.schemas) &&
@@ -129,13 +118,12 @@ class SchemaHandler {
129118
return this.modelReferences[name];
130119
}
131120

132-
const dereferencedSchema = await this.__dereferenceSchema(schema).catch(
133-
(err) => {
134-
throw err;
135-
}
136-
);
137-
138-
const convertedSchemas = SchemaConvertor.convert(dereferencedSchema, name);
121+
const convertedSchemas = await this.__dereferenceAndConvert(schema, name, {
122+
name,
123+
schema,
124+
}).catch((err) => {
125+
throw err;
126+
});
139127

140128
for (const [schemaName, schemaValue] of Object.entries(
141129
convertedSchemas.schemas
@@ -157,6 +145,32 @@ class SchemaHandler {
157145
return `#/components/schemas/${finalName}`;
158146
}
159147

148+
async __dereferenceAndConvert(schema, name, model) {
149+
this.logger.verbose(`dereferencing model: ${name}`);
150+
const dereferencedSchema = await this.__dereferenceSchema(schema).catch(
151+
(err) => {
152+
this.__checkForHTTPErrorsAndThrow(err, model);
153+
154+
this.__checkForMissingPathAndThrow(err);
155+
156+
return schema;
157+
}
158+
);
159+
160+
this.logger.verbose(
161+
`dereferenced model: ${JSON.stringify(dereferencedSchema)}`
162+
);
163+
164+
this.logger.verbose(`converting model: ${name}`);
165+
const convertedSchemas = SchemaConvertor.convert(dereferencedSchema, name);
166+
167+
this.logger.verbose(
168+
`converted schemas: ${JSON.stringify(convertedSchemas)}`
169+
);
170+
171+
return convertedSchemas;
172+
}
173+
160174
async __dereferenceSchema(schema) {
161175
const bundledSchema = await $RefParser
162176
.bundle(schema, this.refParserOptions)
@@ -238,9 +252,23 @@ class SchemaHandler {
238252
}
239253
}
240254

255+
__checkForMissingPathAndThrow(error) {
256+
if (error.message === "Expected a file path, URL, or object. Got undefined")
257+
throw error;
258+
}
259+
260+
__checkForHTTPErrorsAndThrow(error, model) {
261+
if (error.errors) {
262+
for (const err of error?.errors) {
263+
this.__HTTPError(err, model);
264+
}
265+
} else {
266+
this.__HTTPError(error, model);
267+
}
268+
}
269+
241270
__HTTPError(error, model) {
242271
if (error.message.includes("HTTP ERROR")) {
243-
// throw err;
244272
throw new Error(
245273
`There was an error dereferencing ${
246274
model.name

test/unit/definitionGenerator.spec.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe("DefinitionGenerator", () => {
2323
);
2424

2525
beforeEach(function () {
26-
mockServerless = JSON.parse(JSON.stringify(serverlessMock));
26+
mockServerless = structuredClone(serverlessMock);
2727
Object.assign(mockServerless.service.custom.documentation, modelsDocument);
2828
});
2929

@@ -39,9 +39,7 @@ describe("DefinitionGenerator", () => {
3939
});
4040

4141
it("should default to version 3.0.0 of openAPI when openAPI version is not passed in", function () {
42-
const serverlessWithoutOpenAPIVersion = JSON.parse(
43-
JSON.stringify(mockServerless)
44-
);
42+
const serverlessWithoutOpenAPIVersion = structuredClone(mockServerless);
4543
delete serverlessWithoutOpenAPIVersion.processedInput;
4644
let expected = new DefinitionGenerator(
4745
serverlessWithoutOpenAPIVersion,
@@ -108,9 +106,7 @@ describe("DefinitionGenerator", () => {
108106
});
109107

110108
it("should respect the version of openAPI when passed in", function () {
111-
const serverlessWithOpenAPIVersion = JSON.parse(
112-
JSON.stringify(mockServerless)
113-
);
109+
const serverlessWithOpenAPIVersion = structuredClone(mockServerless);
114110
serverlessWithOpenAPIVersion.processedInput.options.openApiVersion =
115111
"3.0.2";
116112
let expected = new DefinitionGenerator(

test/unit/openAPIGenerator.spec.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ describe("OpenAPIGenerator", () => {
107107
const getAllFuncsStub = sinon
108108
.stub(sls.service, "getAllFunctions")
109109
.returns(["createUser"]);
110-
const basicInvalidFunction = JSON.parse(
111-
JSON.stringify(basicValidFunction)
112-
);
110+
const basicInvalidFunction = structuredClone(basicValidFunction);
113111

114112
delete basicInvalidFunction.createUser.events[0].http.documentation
115113
.methodResponses[0].responseModels;
@@ -145,9 +143,7 @@ describe("OpenAPIGenerator", () => {
145143
const getAllFuncsStub = sinon
146144
.stub(sls.service, "getAllFunctions")
147145
.returns(["createUser"]);
148-
const basicInvalidFunction = JSON.parse(
149-
JSON.stringify(basicValidFunction)
150-
);
146+
const basicInvalidFunction = structuredClone(basicValidFunction);
151147

152148
const getFuncStub = sinon
153149
.stub(sls.service, "getFunction")
@@ -182,9 +178,7 @@ describe("OpenAPIGenerator", () => {
182178
.stub(sls.service, "getAllFunctions")
183179
.returns(["createUser"]);
184180

185-
const basicInvalidFunction = JSON.parse(
186-
JSON.stringify(basicValidFunction)
187-
);
181+
const basicInvalidFunction = structuredClone(basicValidFunction);
188182

189183
delete basicInvalidFunction.createUser.events[0].http.documentation
190184
.pathParams;

test/unit/owasp.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe(`owasp`, function () {
5656
});
5757

5858
it(`adds any properties contained in a new release`, async function () {
59-
const newOWASPJSONAdded = JSON.parse(JSON.stringify(newOWASPJSON));
59+
const newOWASPJSONAdded = structuredClone(newOWASPJSON);
6060
newOWASPJSONAdded.headers.push({ name: "x-added", value: "true" });
6161

6262
nock("https://owasp.org")

test/unit/schemaHandler.spec.js

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ describe(`SchemaHandler`, function () {
4343
};
4444

4545
beforeEach(function () {
46-
mockServerless = JSON.parse(JSON.stringify(serverlessMock));
47-
modelsDocument = JSON.parse(JSON.stringify(modelsDocumentOG));
48-
modelsAltDocument = JSON.parse(JSON.stringify(modelsAltDocumentOG));
49-
modelsListDocument = JSON.parse(JSON.stringify(modelsListDocumentOG));
50-
modelsListAltDocument = JSON.parse(JSON.stringify(modelsListAltDocumentOG));
51-
openAPI = JSON.parse(JSON.stringify(openAPISchema));
46+
mockServerless = structuredClone(serverlessMock);
47+
modelsDocument = structuredClone(modelsDocumentOG);
48+
modelsAltDocument = structuredClone(modelsAltDocumentOG);
49+
modelsListDocument = structuredClone(modelsListDocumentOG);
50+
modelsListAltDocument = structuredClone(modelsListAltDocumentOG);
51+
openAPI = structuredClone(openAPISchema);
5252
});
5353

5454
describe(`constuctor`, function () {
@@ -135,7 +135,7 @@ describe(`SchemaHandler`, function () {
135135
});
136136

137137
it(`should standardise mixed models syntax in to the correct format`, function () {
138-
const newModelsDocument = JSON.parse(JSON.stringify(modelsDocument));
138+
const newModelsDocument = structuredClone(modelsDocument);
139139
Object.assign(
140140
mockServerless.service.custom.documentation,
141141
newModelsDocument
@@ -168,9 +168,7 @@ describe(`SchemaHandler`, function () {
168168
});
169169

170170
it(`should standardise mixed modelsList syntax in to the correct format`, function () {
171-
const newModelsDocument = JSON.parse(
172-
JSON.stringify(modelsListDocument)
173-
);
171+
const newModelsDocument = structuredClone(modelsListDocument);
174172
Object.assign(
175173
mockServerless.service.custom.documentation,
176174
newModelsDocument
@@ -203,9 +201,7 @@ describe(`SchemaHandler`, function () {
203201
});
204202

205203
it(`should standardise mixed models and modelsList syntax in to the correct format`, function () {
206-
const newModelsDocument = JSON.parse(
207-
JSON.stringify(modelsListDocument)
208-
);
204+
const newModelsDocument = structuredClone(modelsListDocument);
209205
Object.assign(
210206
mockServerless.service.custom.documentation,
211207
newModelsDocument

0 commit comments

Comments
 (0)