diff --git a/features/step_definitions/request_steps.ts b/features/step_definitions/request_steps.ts index 3f4755de2309..7ccb6e5389e7 100644 --- a/features/step_definitions/request_steps.ts +++ b/features/step_definitions/request_steps.ts @@ -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"; @@ -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( @@ -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); } ); diff --git a/features/support/templating.ts b/features/support/templating.ts index a77c4a429208..cd3ed5b028d6 100644 --- a/features/support/templating.ts +++ b/features/support/templating.ts @@ -1,3 +1,5 @@ +import { UnparsedObject } from "../../packages/datadog-api-client-common/util"; + declare global { interface String { templated(data: { [key: string]: any }): string; @@ -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( @@ -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 = /^(.+)\((.*)\)$/; @@ -120,4 +138,4 @@ function getProperty(obj: T, name: string): T[K] { return obj[key]; } -export { pathLookup, getProperty }; +export { pathLookup, getProperty, getTypeForValue };