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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/http-specs"
---

Accept both `2022-08-26T18:38:00.000Z` and `2022-08-26T18:38:00Z` as valid RFC3339 UTC datetime forms in the `ModelWithDatetime` XML scenario.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/spec-api"
---

Remove prettier used for ValidationError message, in validateXmlBodyEquals.
54 changes: 48 additions & 6 deletions packages/http-specs/specs/payload/xml/mockapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ export const modelWithDatetime = `
</ModelWithDatetime>
`;

// Some clients serialize UTC datetimes without trailing zero milliseconds. Both
// "2022-08-26T18:38:00.000Z" and "2022-08-26T18:38:00Z" are valid RFC3339 representations
// of the same instant; accept either form.
const modelWithDatetimeNoMs = `
<ModelWithDatetime>
<rfc3339>2022-08-26T18:38:00Z</rfc3339>
<rfc7231>Fri, 26 Aug 2022 14:38:00 GMT</rfc7231>
</ModelWithDatetime>
`;

function createServerTests(uri: string, data?: any) {
return {
get: passOnSuccess({
Expand Down Expand Up @@ -251,12 +261,44 @@ const Payload_Xml_ModelWithEnum = createServerTests("/payload/xml/modelWithEnum"
Scenarios.Payload_Xml_ModelWithEnumValue_get = Payload_Xml_ModelWithEnum.get;
Scenarios.Payload_Xml_ModelWithEnumValue_put = Payload_Xml_ModelWithEnum.put;

const Payload_Xml_ModelWithDatetime = createServerTests(
"/payload/xml/modelWithDatetime",
modelWithDatetime,
);
Scenarios.Payload_Xml_ModelWithDatetimeValue_get = Payload_Xml_ModelWithDatetime.get;
Scenarios.Payload_Xml_ModelWithDatetimeValue_put = Payload_Xml_ModelWithDatetime.put;
Scenarios.Payload_Xml_ModelWithDatetimeValue_get = passOnSuccess({
uri: "/payload/xml/modelWithDatetime",
method: "get",
request: {},
response: {
status: 200,
body: xml(modelWithDatetime),
},
kind: "MockApiDefinition",
});

Scenarios.Payload_Xml_ModelWithDatetimeValue_put = passOnSuccess({
uri: "/payload/xml/modelWithDatetime",
method: "put",
request: {
body: xml(modelWithDatetime),
},
handler: (req: MockRequest) => {
req.expect.containsHeader("content-type", "application/xml");
// Accept both "2022-08-26T18:38:00.000Z" and "2022-08-26T18:38:00Z" as equivalent UTC datetimes.
let firstError: unknown;
try {
req.expect.xmlBodyEquals(modelWithDatetime);
} catch (e) {
firstError = e;
}
if (firstError !== undefined) {
req.expect.xmlBodyEquals(modelWithDatetimeNoMs);
}
return {
status: 204,
};
},
response: {
status: 204,
},
kind: "MockApiDefinition",
});

export const xmlError = `
<XmlErrorBody>
Expand Down
7 changes: 1 addition & 6 deletions packages/spec-api/src/request-validations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import deepEqual from "deep-equal";
import * as prettier from "prettier";
import { parseString } from "xml2js";
import { CollectionFormat, RequestExt } from "./types.js";
import { ValidationError } from "./validation-error.js";
Expand Down Expand Up @@ -66,11 +65,7 @@ export const validateXmlBodyEquals = (request: RequestExt, expectedBody: string)
});

if (!deepEqual(actualParsedBody, expectedParsedBody, { strict: true })) {
throw new ValidationError(
BODY_NOT_EQUAL_ERROR_MESSAGE,
prettier.format(expectedBody),
prettier.format(request.body),
);
throw new ValidationError(BODY_NOT_EQUAL_ERROR_MESSAGE, expectedBody, request.rawBody);
}
};

Expand Down
Loading