Skip to content

Commit a7d6aac

Browse files
committed
fix(codegen): for static schema
1 parent 761d89c commit a7d6aac

File tree

9 files changed

+59
-56
lines changed

9 files changed

+59
-56
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@smithy/core": patch
3+
"@smithy/types": patch
4+
---
5+
6+
fix ordering of static simple schema type

packages/core/src/submodules/cbor/SmithyRpcV2CborProtocol.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
OperationSchema,
1111
ResponseMetadata,
1212
SerdeFunctions,
13+
StaticErrorSchema,
1314
} from "@smithy/types";
1415
import { getSmithyContext } from "@smithy/util-middleware";
1516

@@ -104,9 +105,9 @@ export class SmithyRpcV2CborProtocol extends RpcProtocol {
104105

105106
const registry = TypeRegistry.for(namespace);
106107

107-
let errorSchema: ErrorSchema;
108+
let errorSchema: StaticErrorSchema;
108109
try {
109-
errorSchema = registry.getSchema(errorName) as ErrorSchema;
110+
errorSchema = registry.getSchema(errorName) as StaticErrorSchema;
110111
} catch (e) {
111112
if (dataObject.Message) {
112113
dataObject.message = dataObject.Message;

packages/core/src/submodules/schema/TypeRegistry.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export class TypeRegistry {
3535
*/
3636
public register(shapeId: string, schema: ISchema) {
3737
const qualifiedName = this.normalizeShapeId(shapeId);
38-
this.schemas.set(qualifiedName, schema);
38+
const registry = TypeRegistry.for(qualifiedName.split("#")[0]);
39+
registry.schemas.set(qualifiedName, schema);
3940
}
4041

4142
/**
@@ -54,15 +55,20 @@ export class TypeRegistry {
5455
* Associates an error schema with its constructor.
5556
*/
5657
public registerError(es: ErrorSchema | StaticErrorSchema, ctor: any) {
57-
this.exceptions.set(es, ctor);
58+
const $error = es as StaticErrorSchema;
59+
const registry = TypeRegistry.for($error[1]);
60+
registry.schemas.set($error[1] + "#" + $error[2], $error);
61+
registry.exceptions.set($error, ctor);
5862
}
5963

6064
/**
6165
* @param es - query.
6266
* @returns Error constructor that extends the service's base exception.
6367
*/
6468
public getErrorCtor(es: ErrorSchema | StaticErrorSchema): any {
65-
return this.exceptions.get(es);
69+
const $error = es as StaticErrorSchema;
70+
const registry = TypeRegistry.for($error[1]);
71+
return registry.exceptions.get(es);
6672
}
6773

6874
/**
@@ -78,10 +84,14 @@ export class TypeRegistry {
7884
*
7985
* @returns the synthetic base exception of the service namespace associated with this registry instance.
8086
*/
81-
public getBaseException(): ErrorSchema | undefined {
82-
for (const [id, schema] of this.schemas.entries()) {
83-
if (id.startsWith("smithy.ts.sdk.synthetic.") && id.endsWith("ServiceException")) {
84-
return schema as ErrorSchema;
87+
public getBaseException(): StaticErrorSchema | undefined {
88+
for (const exceptionKey of this.exceptions.keys()) {
89+
if (Array.isArray(exceptionKey)) {
90+
const [, ns, name] = exceptionKey;
91+
const id = ns + "#" + name;
92+
if (id.startsWith("smithy.ts.sdk.synthetic.") && id.endsWith("ServiceException")) {
93+
return exceptionKey;
94+
}
8595
}
8696
}
8797
return undefined;
@@ -109,8 +119,4 @@ export class TypeRegistry {
109119
}
110120
return this.namespace + "#" + shapeId;
111121
}
112-
113-
private getNamespace(shapeId: string) {
114-
return this.normalizeShapeId(shapeId).split("#")[0];
115-
}
116122
}

packages/core/src/submodules/schema/schemas/NormalizedSchema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import type { OperationSchema } from "./OperationSchema";
4343
import { op } from "./OperationSchema";
4444
import { Schema } from "./Schema";
4545
import type { SimpleSchema } from "./SimpleSchema";
46-
import { sim } from "./SimpleSchema";
46+
import { simAdapter } from "./SimpleSchema";
4747
import { struct, StructureSchema } from "./StructureSchema";
4848
import { translateTraits } from "./translateTraits";
4949

@@ -450,7 +450,7 @@ export function hydrate(
450450
const [id, ...rest] = ss;
451451
return (
452452
{
453-
[0 satisfies StaticSchemaIdSimple]: sim,
453+
[0 satisfies StaticSchemaIdSimple]: simAdapter,
454454
[1 satisfies StaticSchemaIdList]: list,
455455
[2 satisfies StaticSchemaIdMap]: map,
456456
[3 satisfies StaticSchemaIdStruct]: struct,

packages/core/src/submodules/schema/schemas/Schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export abstract class Schema implements TraitsSchema {
1515

1616
public static assign<T extends Schema>(instance: T, values: Omit<T, "getName" | "symbol">): T {
1717
const schema = Object.assign(instance, values);
18-
TypeRegistry.for(schema.namespace).register(schema.name, schema);
18+
// TypeRegistry.for(schema.namespace).register(schema.name, schema);
1919
return schema;
2020
}
2121

packages/core/src/submodules/schema/schemas/SimpleSchema.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,15 @@ export const sim = (namespace: string, name: string, schemaRef: SchemaRef, trait
2929
traits,
3030
schemaRef,
3131
});
32+
33+
/**
34+
* @internal
35+
* @deprecated
36+
*/
37+
export const simAdapter = (namespace: string, name: string, traits: SchemaTraits, schemaRef: SchemaRef) =>
38+
Schema.assign(new SimpleSchema(), {
39+
name,
40+
namespace,
41+
traits,
42+
schemaRef,
43+
});

packages/types/src/schema/static-schemas.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export type ShapeNamespace = string;
5959
/**
6060
* @alpha
6161
*/
62-
export type StaticSimpleSchema = [StaticSchemaIdSimple, ShapeNamespace, ShapeName, SchemaRef, SchemaTraits];
62+
export type StaticSimpleSchema = [StaticSchemaIdSimple, ShapeNamespace, ShapeName, SchemaTraits, SchemaRef];
6363

6464
/**
6565
* @alpha

private/smithy-rpcv2-cbor-schema/src/schemas/schemas_0.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ import {
137137
InvalidGreeting as __InvalidGreeting,
138138
ValidationException as __ValidationException,
139139
} from "../models/index";
140-
import { TypeRegistry, error } from "@smithy/core/schema";
140+
import { TypeRegistry } from "@smithy/core/schema";
141141
import {
142142
StaticErrorSchema,
143143
StaticListSchema,
@@ -295,26 +295,15 @@ export var RpcV2CborDenseMapsInputOutput: StaticStructureSchema = [
295295
_RVCDMIO,
296296
0,
297297
[_dSM, _dNM, _dBM, _dSMe, _dSMen],
298-
[() => DenseStructMap, 128 | 1, 128 | 2, 128 | 0, [2, n1, _DSM, 0, 64 | 0, 64 | 0] as StaticMapSchema],
298+
[() => DenseStructMap, 128 | 1, 128 | 2, 128 | 0, [2, n1, _DSM, 0, 0, 64 | 0]],
299299
];
300300
export var RpcV2CborListInputOutput: StaticStructureSchema = [
301301
3,
302302
n1,
303303
_RVCLIO,
304304
0,
305305
[_sL, _sS, _iL, _bL, _tL, _eL, _iEL, _nSL, _sLt, _bLl],
306-
[
307-
64 | 0,
308-
64 | 0,
309-
64 | 1,
310-
64 | 2,
311-
64 | 4,
312-
64 | 0,
313-
64 | 1,
314-
[1, n2, _NSL, 0, 64 | 0] as StaticListSchema,
315-
() => StructureList,
316-
64 | 21,
317-
],
306+
[64 | 0, 64 | 0, 64 | 1, 64 | 2, 64 | 4, 64 | 0, 64 | 1, [1, n2, _NSL, 0, 64 | 0], () => StructureList, 64 | 21],
318307
];
319308
export var RpcV2CborSparseMapsInputOutput: StaticStructureSchema = [
320309
3,
@@ -352,7 +341,7 @@ export var SparseNullsOperationInputOutput: StaticStructureSchema = [
352341
];
353342
export var StructureListMember: StaticStructureSchema = [3, n1, _SLM, 0, [_a, _b_], [0, 0]];
354343
export var GreetingStruct: StaticStructureSchema = [3, n2, _GS, 0, [_h], [0]];
355-
export var RpcV2ProtocolServiceException = error(_sC, "RpcV2ProtocolServiceException", 0, [], [], null);
344+
export var RpcV2ProtocolServiceException: StaticErrorSchema = [-3, _sC, "RpcV2ProtocolServiceException", 0, [], []];
356345
TypeRegistry.for(_sC).registerError(RpcV2ProtocolServiceException, __RpcV2ProtocolServiceException);
357346

358347
export var ValidationExceptionFieldList: StaticListSchema = [1, n0, _VEFL, 0, () => ValidationExceptionField];

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/schema/SchemaGenerator.java

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -276,15 +276,17 @@ private void writeSimpleSchema(Shape shape) {
276276
TypeScriptWriter writer = getWriter(shape.getId());
277277
if (elision.traits.hasSchemaTraits(shape)) {
278278
writer.addImport("StaticSimpleSchema", null, TypeScriptDependency.SMITHY_TYPES);
279-
writer.write("""
280-
export var $L: StaticSimpleSchema = [0, $L, $L, $L,""",
279+
writer.openBlock("""
280+
export var $L: StaticSimpleSchema = [0, $L, $L,""",
281+
", $L];",
281282
getShapeVariableName(shape),
282283
checkImportString(shape, shape.getId().getNamespace(), "n"),
283284
checkImportString(shape, shape.getId().getName()),
284-
resolveSimpleSchema(shape, shape)
285+
resolveSimpleSchema(shape, shape),
286+
() -> {
287+
writeTraits(shape);
288+
}
285289
);
286-
writeTraits(shape);
287-
writer.write("];");
288290
}
289291
}
290292

@@ -341,7 +343,7 @@ private void writeBaseError() {
341343
String namespace = settings.getService(model).getId().getNamespace();
342344

343345
String exceptionCtorSymbolName = "__" + serviceExceptionName;
344-
writer.addImportSubmodule("error", null, TypeScriptDependency.SMITHY_CORE, "/schema");
346+
writer.addImport("StaticErrorSchema", null, TypeScriptDependency.SMITHY_TYPES);
345347
writer.addRelativeImport(
346348
serviceExceptionName,
347349
exceptionCtorSymbolName,
@@ -350,7 +352,7 @@ private void writeBaseError() {
350352

351353
String syntheticNamespace = store.var("smithy.ts.sdk.synthetic." + namespace);
352354
writer.write("""
353-
export var $L = error($L, $S, 0, [], [], null);""",
355+
export var $L: StaticErrorSchema = [-3, $L, $S, 0, [], []];""",
354356
serviceExceptionName,
355357
syntheticNamespace,
356358
serviceExceptionName
@@ -672,25 +674,19 @@ private String resolveSimpleSchemaNestedContainer(Shape context, Shape shape, Ty
672674
String staticTypePrefix;
673675
String sentinel;
674676
String keySchema = "";
675-
String valueSchema;
676-
String as;
677677
switch (shape.getType()) {
678678
case LIST -> {
679679
contained = shape.asListShape().get().getMember();
680-
staticTypePrefix = "([1, ";
681-
valueSchema = "";
680+
staticTypePrefix = "[1, ";
682681
sentinel = "64";
683682
writer.addImport("StaticListSchema", null, TypeScriptDependency.SMITHY_TYPES);
684-
as = " as StaticListSchema)";
685683
}
686684
case MAP -> {
687685
contained = shape.asMapShape().get().getValue();
688-
staticTypePrefix = "([2, ";
686+
staticTypePrefix = "[2, ";
689687
keySchema = this.resolveSimpleSchema(context, shape.asMapShape().get().getKey()) + ", ";
690-
valueSchema = this.resolveSimpleSchema(context, shape.asMapShape().get().getValue()) + ", ";
691688
sentinel = "128";
692689
writer.addImport("StaticMapSchema", null, TypeScriptDependency.SMITHY_TYPES);
693-
as = " as StaticMapSchema)";
694690
}
695691
default -> {
696692
throw new IllegalArgumentException(
@@ -702,19 +698,12 @@ private String resolveSimpleSchemaNestedContainer(Shape context, Shape shape, Ty
702698
contained = model.expectShape(contained.asMemberShape().get().getTarget());
703699
}
704700

705-
if (contained.isListShape()) {
706-
String schemaVarName = checkImportString(context, shape.getId().getName());
707-
return staticTypePrefix
708-
+ checkImportString(context, shape.getId().getNamespace(), "n") + ", " + schemaVarName + ", 0, "
709-
+ valueSchema
710-
+ this.resolveSimpleSchema(context, contained) + "]" + as;
711-
} else if (contained.isMapShape()) {
701+
if (contained.isListShape() || contained.isMapShape()) {
712702
String schemaVarName = checkImportString(context, shape.getId().getName());
713703
return staticTypePrefix
714704
+ checkImportString(context, shape.getId().getNamespace(), "n") + ", " + schemaVarName + ", 0, "
715705
+ keySchema
716-
+ valueSchema
717-
+ this.resolveSimpleSchema(context, contained) + "]" + as;
706+
+ this.resolveSimpleSchema(context, contained) + "]";
718707
} else {
719708
return sentinel + "|" + this.resolveSimpleSchema(context, contained);
720709
}

0 commit comments

Comments
 (0)