Skip to content

Commit c329865

Browse files
fix(spec-api): correct ModelWithDatetime rfc3339 value and remove invalid prettier call in XML validation (#9995)
- [x] Revert `normalizeXmlDatetimes` from `spec-api/src/request-validations.ts` (restore plain `deepEqual`) — no prettier issues (only deletions + shortening an existing line) - [x] In `mockapi.ts`, add `modelWithDatetimeNoMs` constant and custom PUT handler for `ModelWithDatetime` accepting both `.000Z` and bare `Z` forms - [x] Add changelog entry for `@typespec/http-specs` and `@typespec/spec-api` <!-- START COPILOT CODING AGENT TIPS --> --- 💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more [Copilot coding agent tips](https://gh.io/copilot-coding-agent-tips) in the docs. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: weidongxu-microsoft <53292327+weidongxu-microsoft@users.noreply.github.com> Co-authored-by: Weidong Xu <weidxu@microsoft.com>
1 parent 381ea03 commit c329865

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/http-specs"
5+
---
6+
7+
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.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/spec-api"
5+
---
6+
7+
Remove prettier used for ValidationError message, in validateXmlBodyEquals.

packages/http-specs/specs/payload/xml/mockapi.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ export const modelWithDatetime = `
136136
</ModelWithDatetime>
137137
`;
138138

139+
// Some clients serialize UTC datetimes without trailing zero milliseconds. Both
140+
// "2022-08-26T18:38:00.000Z" and "2022-08-26T18:38:00Z" are valid RFC3339 representations
141+
// of the same instant; accept either form.
142+
const modelWithDatetimeNoMs = `
143+
<ModelWithDatetime>
144+
<rfc3339>2022-08-26T18:38:00Z</rfc3339>
145+
<rfc7231>Fri, 26 Aug 2022 14:38:00 GMT</rfc7231>
146+
</ModelWithDatetime>
147+
`;
148+
139149
function createServerTests(uri: string, data?: any) {
140150
return {
141151
get: passOnSuccess({
@@ -251,12 +261,44 @@ const Payload_Xml_ModelWithEnum = createServerTests("/payload/xml/modelWithEnum"
251261
Scenarios.Payload_Xml_ModelWithEnumValue_get = Payload_Xml_ModelWithEnum.get;
252262
Scenarios.Payload_Xml_ModelWithEnumValue_put = Payload_Xml_ModelWithEnum.put;
253263

254-
const Payload_Xml_ModelWithDatetime = createServerTests(
255-
"/payload/xml/modelWithDatetime",
256-
modelWithDatetime,
257-
);
258-
Scenarios.Payload_Xml_ModelWithDatetimeValue_get = Payload_Xml_ModelWithDatetime.get;
259-
Scenarios.Payload_Xml_ModelWithDatetimeValue_put = Payload_Xml_ModelWithDatetime.put;
264+
Scenarios.Payload_Xml_ModelWithDatetimeValue_get = passOnSuccess({
265+
uri: "/payload/xml/modelWithDatetime",
266+
method: "get",
267+
request: {},
268+
response: {
269+
status: 200,
270+
body: xml(modelWithDatetime),
271+
},
272+
kind: "MockApiDefinition",
273+
});
274+
275+
Scenarios.Payload_Xml_ModelWithDatetimeValue_put = passOnSuccess({
276+
uri: "/payload/xml/modelWithDatetime",
277+
method: "put",
278+
request: {
279+
body: xml(modelWithDatetime),
280+
},
281+
handler: (req: MockRequest) => {
282+
req.expect.containsHeader("content-type", "application/xml");
283+
// Accept both "2022-08-26T18:38:00.000Z" and "2022-08-26T18:38:00Z" as equivalent UTC datetimes.
284+
let firstError: unknown;
285+
try {
286+
req.expect.xmlBodyEquals(modelWithDatetime);
287+
} catch (e) {
288+
firstError = e;
289+
}
290+
if (firstError !== undefined) {
291+
req.expect.xmlBodyEquals(modelWithDatetimeNoMs);
292+
}
293+
return {
294+
status: 204,
295+
};
296+
},
297+
response: {
298+
status: 204,
299+
},
300+
kind: "MockApiDefinition",
301+
});
260302

261303
export const xmlError = `
262304
<XmlErrorBody>

packages/spec-api/src/request-validations.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import deepEqual from "deep-equal";
2-
import * as prettier from "prettier";
32
import { parseString } from "xml2js";
43
import { CollectionFormat, RequestExt } from "./types.js";
54
import { ValidationError } from "./validation-error.js";
@@ -66,11 +65,7 @@ export const validateXmlBodyEquals = (request: RequestExt, expectedBody: string)
6665
});
6766

6867
if (!deepEqual(actualParsedBody, expectedParsedBody, { strict: true })) {
69-
throw new ValidationError(
70-
BODY_NOT_EQUAL_ERROR_MESSAGE,
71-
prettier.format(expectedBody),
72-
prettier.format(request.body),
73-
);
68+
throw new ValidationError(BODY_NOT_EQUAL_ERROR_MESSAGE, expectedBody, request.rawBody);
7469
}
7570
};
7671

0 commit comments

Comments
 (0)