Skip to content

Commit 51d6c50

Browse files
authored
Deserialize fixture values (#1010)
* deserialize fixture instead * remove lodash import * small cleanup
1 parent 2a3e4f4 commit 51d6c50

File tree

2 files changed

+35
-17
lines changed

2 files changed

+35
-17
lines changed

features/step_definitions/request_steps.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Given, Then, When, AfterAll } from "@cucumber/cucumber";
22
import { expect } from "chai";
33
import { World } from "../support/world";
44

5-
import { getProperty, pathLookup } from "../support/templating";
5+
import { getProperty, pathLookup, getTypeForValue } from "../support/templating";
66
import { Store } from "../support/store";
77
import { buildUndoFor, UndoActions } from "../support/undo";
88
import * as datadogApiClient from "../../index";
@@ -134,13 +134,6 @@ When("the request is sent", async function (this: World) {
134134
this.response = await apiInstance[this.operationId.toOperationName()]();
135135
}
136136

137-
const objectSerializer = getProperty(datadogApiClient, this.apiVersion).ObjectSerializer;
138-
this.response = objectSerializer.serialize(
139-
this.response,
140-
ScenariosModelMappings[`${this.apiVersion}.${this.operationId}`]["operationResponseType"],
141-
"",
142-
)
143-
144137
if (undoAction.undo.type == "unsafe") {
145138
this.undo.push(
146139
buildUndoFor(
@@ -262,9 +255,16 @@ Then(
262255
Then(
263256
/the response "([^"]+)" is equal to (.*)/,
264257
function (this: World, responsePath: string, value: string) {
265-
expect(pathLookup(this.response, responsePath)).to.deep.equal(
266-
JSON.parse(value.templated(this.fixtures))
267-
);
258+
const pathResult = pathLookup(this.response, responsePath)
259+
const _type = getTypeForValue(pathResult)
260+
let templatedFixtureValue = JSON.parse(value.templated(this.fixtures))
261+
262+
if (_type) {
263+
const objectSerializer = getProperty(datadogApiClient, this.apiVersion).ObjectSerializer;
264+
templatedFixtureValue = objectSerializer.deserialize(templatedFixtureValue, _type, "")
265+
}
266+
267+
expect(pathResult).to.deep.equal(templatedFixtureValue);
268268
}
269269
);
270270

features/support/templating.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { UnparsedObject } from "../../packages/datadog-api-client-common/util";
2+
13
declare global {
24
interface String {
35
templated(data: { [key: string]: any }): string;
@@ -68,11 +70,8 @@ function pathLookup(data: any, dottedPath: string): any {
6870
result = value[part];
6971
} else if (part.toAttributeName() in value) {
7072
result = value[part.toAttributeName()];
71-
} else if (
72-
"unparsedObject" in value &&
73-
part in value["unparsedObject"]
74-
) {
75-
result = value["unparsedObject"][part];
73+
} else if (value instanceof UnparsedObject && part in value["_data"]) {
74+
result = value["_data"][part];
7675
} else {
7776
throw new Error(
7877
`${part} not found in ${JSON.stringify(
@@ -81,11 +80,30 @@ function pathLookup(data: any, dottedPath: string): any {
8180
);
8281
}
8382
}
83+
if (result instanceof UnparsedObject) {
84+
result = result["_data"]
85+
}
8486
}
8587
}
88+
8689
return result;
8790
}
8891

92+
function getTypeForValue(pathResult: any): string {
93+
let _type: string = "";
94+
95+
if (pathResult?.constructor?.name) {
96+
if (pathResult.constructor?.name == "Array") {
97+
if (pathResult[0]?.constructor?.name) {
98+
_type = `Array<${pathResult[0].constructor.name}>`
99+
}
100+
} else {
101+
_type = pathResult.constructor.name
102+
}
103+
}
104+
return _type;
105+
}
106+
89107
String.prototype.templated = function (data: { [key: string]: any }): string {
90108
const regexp = /{{ *([^{}]+|'[^']+'|"[^"]+") *}}/g;
91109
const function_re = /^(.+)\((.*)\)$/;
@@ -120,4 +138,4 @@ function getProperty<T, K extends keyof T>(obj: T, name: string): T[K] {
120138
return obj[key];
121139
}
122140

123-
export { pathLookup, getProperty };
141+
export { pathLookup, getProperty, getTypeForValue };

0 commit comments

Comments
 (0)