Skip to content

Commit 097a8f6

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

File tree

6 files changed

+462
-24
lines changed

6 files changed

+462
-24
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 & 10 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,11 +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
}
42+
// non-struct root schema indicates a blob (base64 string) or plain string payload.
3043
return this.buffer;
3144
}
3245

@@ -73,23 +86,21 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
7386
return void 0;
7487
}
7588

76-
if (ns.isBlobSchema() && (value instanceof Uint8Array || typeof value === "string")) {
89+
if (
90+
(ns.isBlobSchema() && (value instanceof Uint8Array || typeof value === "string")) ||
91+
(ns.isDocumentSchema() && value instanceof Uint8Array)
92+
) {
7793
if (ns === this.rootSchema) {
7894
return value;
7995
}
8096
if (!this.serdeContext?.base64Encoder) {
81-
throw new Error("Missing base64Encoder in serdeContext");
97+
return toBase64(value);
8298
}
8399
return this.serdeContext?.base64Encoder(value);
84100
}
85101

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;
102+
if ((ns.isTimestampSchema() || ns.isDocumentSchema()) && value instanceof Date) {
103+
const format = determineTimestampFormat(ns, this.settings);
93104
switch (format) {
94105
case SCHEMA.TIMESTAMP_DATE_TIME:
95106
return value.toISOString().replace(".000Z", "Z");
@@ -124,6 +135,10 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
124135
}
125136
}
126137

138+
if (ns.isDocumentSchema() && isObject) {
139+
return structuredClone(value);
140+
}
141+
127142
return value;
128143
}
129144
}

0 commit comments

Comments
 (0)