Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions features/step_definitions/request_steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Given, Then, When, AfterAll } from "@cucumber/cucumber";
import { expect } from "chai";
import { World } from "../support/world";

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

const objectSerializer = getProperty(datadogApiClient, this.apiVersion).ObjectSerializer;
this.response = objectSerializer.serialize(
this.response,
ScenariosModelMappings[`${this.apiVersion}.${this.operationId}`]["operationResponseType"],
"",
)

if (undoAction.undo.type == "unsafe") {
this.undo.push(
buildUndoFor(
Expand Down Expand Up @@ -262,9 +255,16 @@ Then(
Then(
/the response "([^"]+)" is equal to (.*)/,
function (this: World, responsePath: string, value: string) {
expect(pathLookup(this.response, responsePath)).to.deep.equal(
JSON.parse(value.templated(this.fixtures))
);
const pathResult = pathLookup(this.response, responsePath)
const _type = getTypeForValue(pathResult)
let templatedFixtureValue = JSON.parse(value.templated(this.fixtures))

if (_type) {
const objectSerializer = getProperty(datadogApiClient, this.apiVersion).ObjectSerializer;
templatedFixtureValue = objectSerializer.deserialize(templatedFixtureValue, _type, "")
}

expect(pathResult).to.deep.equal(templatedFixtureValue);
}
);

Expand Down
30 changes: 24 additions & 6 deletions features/support/templating.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { UnparsedObject } from "../../packages/datadog-api-client-common/util";

declare global {
interface String {
templated(data: { [key: string]: any }): string;
Expand Down Expand Up @@ -68,11 +70,8 @@ function pathLookup(data: any, dottedPath: string): any {
result = value[part];
} else if (part.toAttributeName() in value) {
result = value[part.toAttributeName()];
} else if (
"unparsedObject" in value &&
part in value["unparsedObject"]
) {
result = value["unparsedObject"][part];
} else if (value instanceof UnparsedObject && part in value["_data"]) {
result = value["_data"][part];
} else {
throw new Error(
`${part} not found in ${JSON.stringify(
Expand All @@ -81,11 +80,30 @@ function pathLookup(data: any, dottedPath: string): any {
);
}
}
if (result instanceof UnparsedObject) {
result = result["_data"]
}
}
}

return result;
}

function getTypeForValue(pathResult: any): string {
let _type: string = "";

if (pathResult?.constructor?.name) {
if (pathResult.constructor?.name == "Array") {
if (pathResult[0]?.constructor?.name) {
_type = `Array<${pathResult[0].constructor.name}>`
}
} else {
_type = pathResult.constructor.name
}
}
return _type;
}

String.prototype.templated = function (data: { [key: string]: any }): string {
const regexp = /{{ *([^{}]+|'[^']+'|"[^"]+") *}}/g;
const function_re = /^(.+)\((.*)\)$/;
Expand Down Expand Up @@ -120,4 +138,4 @@ function getProperty<T, K extends keyof T>(obj: T, name: string): T[K] {
return obj[key];
}

export { pathLookup, getProperty };
export { pathLookup, getProperty, getTypeForValue };