Skip to content

Commit aff6547

Browse files
committed
test(core/protocols): document testing
1 parent 52fd2ca commit aff6547

File tree

6 files changed

+462
-25
lines changed

6 files changed

+462
-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,
@@ -33,6 +34,22 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe
3334
return this._read(schema, data);
3435
}
3536

37+
/**
38+
* Attempts to use the discriminator on the data instead of requiring a
39+
* schema as input.
40+
*/
41+
public readDocument(data: DocumentType): any {
42+
if (data && typeof data === "object" && typeof (data as any).__type === "string") {
43+
const object = structuredClone(data as any);
44+
const [namespace, name] = object.__type.split("#");
45+
delete object.__type;
46+
const schema = TypeRegistry.for(namespace).getSchema(name);
47+
const ns = NormalizedSchema.of(schema);
48+
return this.readObject(ns, object);
49+
}
50+
return this.readObject(SCHEMA.DOCUMENT, data);
51+
}
52+
3653
private _read(schema: Schema, value: unknown): any {
3754
const isObject = value !== null && typeof value === "object";
3855

@@ -84,13 +101,8 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe
84101
}
85102
}
86103

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;
104+
if (ns.isTimestampSchema() && value != null) {
105+
const format = determineTimestampFormat(ns, this.settings);
94106
switch (format) {
95107
case SCHEMA.TIMESTAMP_DATE_TIME:
96108
return parseRfc3339DateTimeWithOffset(value);
@@ -126,6 +138,10 @@ export class JsonShapeDeserializer extends SerdeContextConfig implements ShapeDe
126138
}
127139
}
128140

141+
if (ns.isDocumentSchema()) {
142+
return structuredClone(value);
143+
}
144+
129145
// covers string, numeric, boolean, document, bigDecimal
130146
return value;
131147
}

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

Lines changed: 25 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";
23
import { dateToUtcString, generateIdempotencyToken, LazyJsonString } 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,12 +24,22 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
2224
this.buffer = this._write(this.rootSchema, value);
2325
}
2426

27+
public writeDiscriminatedDocument(schema: Schema, value: unknown): void {
28+
this.write(schema, value);
29+
if (typeof this.buffer === "object") {
30+
this.buffer.__type = NormalizedSchema.of(schema).getName(true);
31+
}
32+
}
33+
2534
public flush(): string {
26-
if (this.rootSchema?.isStructSchema() || this.rootSchema?.isDocumentSchema()) {
35+
const { rootSchema } = this;
36+
this.rootSchema = undefined;
37+
38+
if (rootSchema?.isStructSchema() || rootSchema?.isDocumentSchema()) {
2739
const replacer = new JsonReplacer();
2840
return replacer.replaceInJson(JSON.stringify(this.buffer, replacer.createReplacer(), 0));
2941
}
30-
return this.buffer;
42+
return JSON.stringify(this.buffer);
3143
}
3244

3345
private _write(schema: Schema, value: unknown, container?: NormalizedSchema): any {
@@ -73,23 +85,21 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
7385
return void 0;
7486
}
7587

76-
if (ns.isBlobSchema() && (value instanceof Uint8Array || typeof value === "string")) {
88+
if (
89+
(ns.isBlobSchema() && (value instanceof Uint8Array || typeof value === "string")) ||
90+
(ns.isDocumentSchema() && value instanceof Uint8Array)
91+
) {
7792
if (ns === this.rootSchema) {
7893
return value;
7994
}
8095
if (!this.serdeContext?.base64Encoder) {
81-
throw new Error("Missing base64Encoder in serdeContext");
96+
return toBase64(value);
8297
}
8398
return this.serdeContext?.base64Encoder(value);
8499
}
85100

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;
101+
if ((ns.isTimestampSchema() || ns.isDocumentSchema()) && value instanceof Date) {
102+
const format = determineTimestampFormat(ns, this.settings);
93103
switch (format) {
94104
case SCHEMA.TIMESTAMP_DATE_TIME:
95105
return value.toISOString().replace(".000Z", "Z");
@@ -124,6 +134,10 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
124134
}
125135
}
126136

137+
if (ns.isDocumentSchema() && isObject) {
138+
return structuredClone(value);
139+
}
140+
127141
return value;
128142
}
129143
}

0 commit comments

Comments
 (0)