Skip to content

Commit 5284b82

Browse files
authored
chore(core/protocols): preparatory fixes for schemas (#7374)
* fix: json shape serializer media type * test: schema-compatible assertions * fix(core/protocols): return early for unit schema in xml deser * fix(core/protocols): error message detection for AwsQuery * fix(core/protocols): awsQuery error shape consistency and ec2 middleware content length * fix(core/protocols): defer content length calculation to middleware * chore: update lockfile * test(middleware-sdk-ec2): undo content length change
1 parent f7eb085 commit 5284b82

File tree

17 files changed

+51
-63
lines changed

17 files changed

+51
-63
lines changed

packages/core/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
"@smithy/smithy-client": "^4.6.4",
9292
"@smithy/types": "^4.5.0",
9393
"@smithy/util-base64": "^4.1.0",
94-
"@smithy/util-body-length-browser": "^4.1.0",
9594
"@smithy/util-middleware": "^4.1.1",
9695
"@smithy/util-utf8": "^4.1.0",
9796
"tslib": "^2.6.2"

packages/core/src/submodules/protocols/ProtocolLib.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,5 @@
11
import { ErrorSchema, NormalizedSchema, TypeRegistry } from "@smithy/core/schema";
2-
import type {
3-
BodyLengthCalculator,
4-
HttpResponse as IHttpResponse,
5-
MetadataBearer,
6-
ResponseMetadata,
7-
SerdeFunctions,
8-
} from "@smithy/types";
9-
import { calculateBodyLength } from "@smithy/util-body-length-browser";
2+
import type { HttpResponse as IHttpResponse, MetadataBearer, ResponseMetadata } from "@smithy/types";
103

114
/**
125
* @internal
@@ -22,23 +15,6 @@ type ErrorMetadataBearer = MetadataBearer & {
2215
* @internal
2316
*/
2417
export class ProtocolLib {
25-
/**
26-
* @param body - to be inspected.
27-
* @param serdeContext - this is a subset type but in practice is the client.config having a property called bodyLengthChecker.
28-
*
29-
* @returns content-length value for the body if possible.
30-
* @throws Error and should be caught and handled if not possible to determine length.
31-
*/
32-
public calculateContentLength(body: any, serdeContext?: SerdeFunctions) {
33-
const bodyLengthCalculator: BodyLengthCalculator =
34-
(
35-
serdeContext as SerdeFunctions & {
36-
bodyLengthChecker?: BodyLengthCalculator;
37-
}
38-
)?.bodyLengthChecker ?? calculateBodyLength;
39-
return String(bodyLengthCalculator(body));
40-
}
41-
4218
/**
4319
* This is only for REST protocols.
4420
*
@@ -108,9 +84,7 @@ export class ProtocolLib {
10884
const errorSchema = getErrorSchema?.(registry, errorName) ?? (registry.getSchema(errorIdentifier) as ErrorSchema);
10985
return { errorSchema, errorMetadata };
11086
} catch (e) {
111-
if (dataObject.Message) {
112-
dataObject.message = dataObject.Message;
113-
}
87+
dataObject.message = dataObject.message ?? dataObject.Message ?? "UnknownError";
11488
const baseExceptionSchema = TypeRegistry.for("smithy.ts.sdk.synthetic." + namespace).getBaseException();
11589
if (baseExceptionSchema) {
11690
const ErrorCtor = baseExceptionSchema.ctor;

packages/core/src/submodules/protocols/json/AwsJson1_0Protocol.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ describe(AwsJson1_0Protocol.name, () => {
115115
);
116116

117117
expect(request.headers).toEqual({
118-
"content-length": "60",
119118
"content-type": "application/x-amz-json-1.0",
120119
"x-amz-target": "JsonRpc10.MyOperation",
121120
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ export abstract class AwsJsonRpcProtocol extends RpcProtocol {
7171
if (deref(operationSchema.input) === "unit" || !request.body) {
7272
request.body = "{}";
7373
}
74-
try {
75-
request.headers["content-length"] = this.mixin.calculateContentLength(request.body, this.serdeContext);
76-
} catch (e) {}
74+
75+
// content-length header is set by the contentLengthMiddleware.
76+
7777
return request;
7878
}
7979

packages/core/src/submodules/protocols/json/AwsRestJsonProtocol.spec.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ describe(AwsRestJsonProtocol.name, () => {
161161
);
162162

163163
expect(request.headers).toEqual({
164-
"content-length": "25",
165164
"content-type": "application/json",
166165
header: "hello",
167166
a: "1",
@@ -265,7 +264,6 @@ describe(AwsRestJsonProtocol.name, () => {
265264

266265
expect(request.headers).toEqual({
267266
"content-type": "application/json",
268-
"content-length": "24",
269267
"header-default-date": "Thu, 01 Jan 1970 00:00:00 GMT",
270268
"header-member-trait-date": "1970-01-01T00:00:00Z",
271269
"header-epoch-seconds": "0",

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,7 @@ export class AwsRestJsonProtocol extends HttpBindingProtocol {
7878
request.body = "{}";
7979
}
8080

81-
if (request.body) {
82-
try {
83-
request.headers["content-length"] = this.mixin.calculateContentLength(request.body, this.serdeContext);
84-
} catch (e) {}
85-
}
81+
// content-length header is set by the contentLengthMiddleware.
8682

8783
return request;
8884
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export class JsonShapeSerializer extends SerdeContextConfig implements ShapeSeri
130130

131131
const mediaType = ns.getMergedTraits().mediaType;
132132

133-
if (typeof value === "string" && mediaType) {
133+
if (value != null && mediaType) {
134134
const isJson = mediaType === "application/json" || mediaType.endsWith("+json");
135135
if (isJson) {
136136
return LazyJsonString.from(value);

packages/core/src/submodules/protocols/query/AwsQueryProtocol.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ export class AwsQueryProtocol extends RpcProtocol {
8282
request.body = request.body.slice(-1);
8383
}
8484

85-
try {
86-
request.headers["content-length"] = this.mixin.calculateContentLength(request.body, this.serdeContext);
87-
} catch (e) {}
85+
// content-length header is set by the contentLengthMiddleware.
86+
8887
return request;
8988
}
9089

@@ -142,6 +141,13 @@ export class AwsQueryProtocol extends RpcProtocol {
142141
): Promise<never> {
143142
const errorIdentifier = this.loadQueryErrorCode(response, dataObject) ?? "Unknown";
144143
const errorData = this.loadQueryError(dataObject);
144+
const message = this.loadQueryErrorMessage(dataObject);
145+
errorData.message = message;
146+
errorData.Error = {
147+
Type: errorData.Type,
148+
Code: errorData.Code,
149+
Message: message,
150+
};
145151

146152
const { errorSchema, errorMetadata } = await this.mixin.getErrorSchemaOrThrowBaseException(
147153
errorIdentifier,
@@ -156,10 +162,11 @@ export class AwsQueryProtocol extends RpcProtocol {
156162
);
157163

158164
const ns = NormalizedSchema.of(errorSchema);
159-
const message = this.loadQueryErrorMessage(dataObject);
160165
const exception = new errorSchema.ctor(message);
161166

162-
const output = {} as any;
167+
const output = {
168+
Error: errorData.Error,
169+
} as any;
163170

164171
for (const [name, member] of ns.structIterator()) {
165172
const target = member.getMergedTraits().xmlName ?? name;

packages/core/src/submodules/protocols/xml/AwsRestXmlProtocol.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ describe(AwsRestXmlProtocol.name, () => {
3737
method: "POST",
3838
headers: {
3939
"content-type": "application/xml",
40-
"content-length": "167",
4140
},
4241
query: {
4342
delete: "",

packages/core/src/submodules/protocols/xml/AwsRestXmlProtocol.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ export class AwsRestXmlProtocol extends HttpBindingProtocol {
7474
}
7575
}
7676

77-
if (request.body) {
78-
try {
79-
request.headers["content-length"] = this.mixin.calculateContentLength(request.body, this.serdeContext);
80-
} catch (e) {}
81-
}
77+
// content-length header is set by the contentLengthMiddleware.
8278

8379
return request;
8480
}

0 commit comments

Comments
 (0)