Skip to content

Commit b5a5579

Browse files
committed
Use structuredClone
1 parent c6db981 commit b5a5579

File tree

6 files changed

+18
-32
lines changed

6 files changed

+18
-32
lines changed

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
);

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)