Skip to content

Commit 9cd9415

Browse files
authored
test(core/protocols): document testing (#7334)
* test(core/protocols): document testing * test(core/protocols): use serde instead of JSON native
1 parent 84f24f5 commit 9cd9415

File tree

6 files changed

+496
-25
lines changed

6 files changed

+496
-25
lines changed

packages/core/src/submodules/protocols/json/JsonShapeDeserializer.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
1+
import { determineTimestampFormat } from "@smithy/core/protocols";
2+
import { NormalizedSchema, SCHEMA, TypeRegistry } from "@smithy/core/schema";
23
import {
34
LazyJsonString,
45
NumericValue,
@@ -84,13 +85,8 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe
8485
}
8586
}
8687

87-
if (ns.isTimestampSchema()) {
88-
const options = this.settings.timestampFormat;
89-
const format = options.useTrait
90-
? ns.getSchema() === SCHEMA.TIMESTAMP_DEFAULT
91-
? options.default
92-
: ns.getSchema() ?? options.default
93-
: options.default;
88+
if (ns.isTimestampSchema() && value != null) {
89+
const format = determineTimestampFormat(ns, this.settings);
9490
switch (format) {
9591
case SCHEMA.TIMESTAMP_DATE_TIME:
9692
return parseRfc3339DateTimeWithOffset(value);
@@ -112,6 +108,10 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe
112108
if (value instanceof NumericValue) {
113109
return value;
114110
}
111+
const untyped = value as any;
112+
if (untyped.type === "bigDecimal" && "string" in untyped) {
113+
return new NumericValue(untyped.string, untyped.type);
114+
}
115115
return new NumericValue(String(value), "bigDecimal");
116116
}
117117

@@ -126,6 +126,22 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe
126126
}
127127
}
128128

129+
if (ns.isDocumentSchema()) {
130+
if (isObject) {
131+
const out = Array.isArray(value) ? [] : ({} as any);
132+
for (const [k, v] of Object.entries(value)) {
133+
if (v instanceof NumericValue) {
134+
out[k] = v;
135+
} else {
136+
out[k] = this._read(ns, v);
137+
}
138+
}
139+
return out;
140+
} else {
141+
return structuredClone(value);
142+
}
143+
}
144+
129145
// covers string, numeric, boolean, document, bigDecimal
130146
return value;
131147
}

packages/core/src/submodules/protocols/json/JsonShapeSerializer.ts

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import { determineTimestampFormat } from "@smithy/core/protocols";
12
import { NormalizedSchema, SCHEMA } from "@smithy/core/schema";
2-
import { dateToUtcString, generateIdempotencyToken, LazyJsonString } from "@smithy/core/serde";
3+
import { dateToUtcString, generateIdempotencyToken, LazyJsonString, NumericValue } from "@smithy/core/serde";
34
import { Schema, ShapeSerializer } from "@smithy/types";
5+
import { toBase64 } from "@smithy/util-base64";
46

57
import { SerdeContextConfig } from "../ConfigurableSerdeContext";
68
import { JsonSettings } from "./JsonCodec";
@@ -22,11 +24,25 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
2224
this.buffer = this._write(this.rootSchema, value);
2325
}
2426

27+
/**
28+
* @internal
29+
*/
30+
public writeDiscriminatedDocument(schema: Schema, value: unknown): void {
31+
this.write(schema, value);
32+
if (typeof this.buffer === "object") {
33+
this.buffer.__type = NormalizedSchema.of(schema).getName(true);
34+
}
35+
}
36+
2537
public flush(): string {
26-
if (this.rootSchema?.isStructSchema() || this.rootSchema?.isDocumentSchema()) {
38+
const { rootSchema } = this;
39+
this.rootSchema = undefined;
40+
41+
if (rootSchema?.isStructSchema() || rootSchema?.isDocumentSchema()) {
2742
const replacer = new JsonReplacer();
2843
return replacer.replaceInJson(JSON.stringify(this.buffer, replacer.createReplacer(), 0));
2944
}
45+
// non-struct root schema indicates a blob (base64 string) or plain string payload.
3046
return this.buffer;
3147
}
3248

@@ -73,23 +89,21 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
7389
return void 0;
7490
}
7591

76-
if (ns.isBlobSchema() && (value instanceof Uint8Array || typeof value === "string")) {
92+
if (
93+
(ns.isBlobSchema() && (value instanceof Uint8Array || typeof value === "string")) ||
94+
(ns.isDocumentSchema() && value instanceof Uint8Array)
95+
) {
7796
if (ns === this.rootSchema) {
7897
return value;
7998
}
8099
if (!this.serdeContext?.base64Encoder) {
81-
throw new Error("Missing base64Encoder in serdeContext");
100+
return toBase64(value);
82101
}
83102
return this.serdeContext?.base64Encoder(value);
84103
}
85104

86-
if (ns.isTimestampSchema() && value instanceof Date) {
87-
const options = this.settings.timestampFormat;
88-
const format = options.useTrait
89-
? ns.getSchema() === SCHEMA.TIMESTAMP_DEFAULT
90-
? options.default
91-
: ns.getSchema() ?? options.default
92-
: options.default;
105+
if ((ns.isTimestampSchema() || ns.isDocumentSchema()) && value instanceof Date) {
106+
const format = determineTimestampFormat(ns, this.settings);
93107
switch (format) {
94108
case SCHEMA.TIMESTAMP_DATE_TIME:
95109
return value.toISOString().replace(".000Z", "Z");
@@ -124,6 +138,22 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
124138
}
125139
}
126140

141+
if (ns.isDocumentSchema()) {
142+
if (isObject) {
143+
const out = Array.isArray(value) ? [] : ({} as any);
144+
for (const [k, v] of Object.entries(value)) {
145+
if (v instanceof NumericValue) {
146+
out[k] = v;
147+
} else {
148+
out[k] = this._write(ns, v);
149+
}
150+
}
151+
return out;
152+
} else {
153+
return structuredClone(value);
154+
}
155+
}
156+
127157
return value;
128158
}
129159
}

0 commit comments

Comments
 (0)