Skip to content

Commit 2aa00e6

Browse files
authored
chore(schema): seed schema trait filter with auth and protocol definition traits (#1620)
1 parent c8d5bb2 commit 2aa00e6

File tree

11 files changed

+408
-88
lines changed

11 files changed

+408
-88
lines changed

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

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,20 @@
1-
const _B = "Boolean";
2-
const _BL = "BooleanList";
3-
const _BLl = "BlobList";
4-
const _Bl = "Blob";
5-
const _By = "Byte";
61
const _CE = "ComplexError";
72
const _CNED = "ComplexNestedErrorData";
83
const _COD = "ClientOptionalDefaults";
9-
const _D = "Double";
10-
const _DBM = "DenseBooleanMap";
11-
const _DNM = "DenseNumberMap";
12-
const _DSM = "DenseStringMap";
13-
const _DSMe = "DenseSetMap";
14-
const _DSMen = "DenseStructMap";
15-
const _DT = "DateTime";
16-
const _De = "Defaults";
4+
const _D = "Defaults";
5+
const _DSM = "DenseSetMap";
6+
const _DSMe = "DenseStructMap";
177
const _EIO = "EmptyInputOutput";
188
const _ES = "EmptyStructure";
19-
const _F = "Float";
20-
const _FE = "FooEnum";
21-
const _FEL = "FooEnumList";
9+
const _F = "Foo";
2210
const _FO = "Float16Output";
2311
const _FS = "FractionalSeconds";
2412
const _FSO = "FractionalSecondsOutput";
2513
const _Fl = "Float16";
26-
const _Fo = "Foo";
2714
const _GS = "GreetingStruct";
2815
const _GWE = "GreetingWithErrors";
2916
const _GWEO = "GreetingWithErrorsOutput";
30-
const _I = "Integer";
31-
const _IE = "IntegerEnum";
32-
const _IEL = "IntegerEnumList";
3317
const _IG = "InvalidGreeting";
34-
const _IL = "IntegerList";
35-
const _L = "Long";
3618
const _M = "Message";
3719
const _N = "Nested";
3820
const _NIO = "NoInputOutput";
@@ -51,30 +33,20 @@ const _RVCL = "RpcV2CborLists";
5133
const _RVCLIO = "RpcV2CborListInputOutput";
5234
const _RVCSM = "RpcV2CborSparseMaps";
5335
const _RVCSMIO = "RpcV2CborSparseMapsInputOutput";
54-
const _S = "String";
5536
const _SBM = "SparseBooleanMap";
56-
const _SL = "StringList";
37+
const _SL = "StructureList";
5738
const _SLM = "StructureListMember";
58-
const _SLt = "StructureList";
5939
const _SNM = "SparseNumberMap";
6040
const _SNO = "SparseNullsOperation";
6141
const _SNOIO = "SparseNullsOperationInputOutput";
62-
const _SS = "StringSet";
42+
const _SS = "SimpleStructure";
6343
const _SSL = "SparseStringList";
64-
const _SSM = "SparseStringMap";
65-
const _SSMp = "SparseSetMap";
66-
const _SSMpa = "SparseStructMap";
44+
const _SSM = "SparseSetMap";
45+
const _SSMp = "SparseStructMap";
46+
const _SSMpa = "SparseStringMap";
6747
const _SSP = "SimpleScalarProperties";
6848
const _SSS = "SimpleScalarStructure";
69-
const _SSi = "SimpleStructure";
70-
const _Sh = "Short";
71-
const _T = "Timestamp";
72-
const _TE = "TestEnum";
73-
const _TIE = "TestIntEnum";
74-
const _TL = "TimestampList";
75-
const _TLo = "TopLevel";
76-
const _TSL = "TestStringList";
77-
const _TSM = "TestStringMap";
49+
const _TL = "TopLevel";
7850
const _VE = "ValidationException";
7951
const _VEF = "ValidationExceptionField";
8052
const _VEFL = "ValidationExceptionFieldList";
@@ -189,15 +161,15 @@ export var ComplexError = error(
189161
{
190162
[_e]: _c,
191163
},
192-
[_TLo, _N],
164+
[_TL, _N],
193165
[0, () => ComplexNestedErrorData],
194166

195167
__ComplexError
196168
);
197-
export var ComplexNestedErrorData = struct(n1, _CNED, 0, [_Fo], [0]);
169+
export var ComplexNestedErrorData = struct(n1, _CNED, 0, [_F], [0]);
198170
export var Defaults = struct(
199171
n1,
200-
_De,
172+
_D,
201173
0,
202174
[
203175
_dS,
@@ -299,7 +271,7 @@ export var RpcV2CborDenseMapsInputOutput = struct(
299271
_RVCDMIO,
300272
0,
301273
[_dSM, _dNM, _dBM, _dSMe, _dSMen],
302-
[() => DenseStructMap, 128 | 1, 128 | 2, 128 | 0, map(n1, _DSMe, 0, 0, 64 | 0)]
274+
[() => DenseStructMap, 128 | 1, 128 | 2, 128 | 0, map(n1, _DSM, 0, 0, 64 | 0)]
303275
);
304276
export var RpcV2CborListInputOutput = struct(
305277
n1,
@@ -328,7 +300,7 @@ export var SimpleScalarStructure = struct(
328300
[_tBV, _fBV, _bV, _dV, _fV, _iV, _lV, _sV, _sVt, _bVl],
329301
[2, 2, 1, 1, 1, 1, 1, 1, 0, 21]
330302
);
331-
export var SimpleStructure = struct(n1, _SSi, 0, [_v], [0]);
303+
export var SimpleStructure = struct(n1, _SS, 0, [_v], [0]);
332304
export var SparseNullsOperationInputOutput = struct(
333305
n1,
334306
_SNOIO,
@@ -350,7 +322,7 @@ export var RpcV2ProtocolServiceException = error(
350322
__RpcV2ProtocolServiceException
351323
);
352324
export var ValidationExceptionFieldList = list(n0, _VEFL, 0, () => ValidationExceptionField);
353-
export var StructureList = list(n1, _SLt, 0, () => StructureListMember);
325+
export var StructureList = list(n1, _SL, 0, () => StructureListMember);
354326
export var TestStringList = 64 | 0;
355327

356328
export var BlobList = 64 | 21;
@@ -382,10 +354,10 @@ export var DenseBooleanMap = 128 | 2;
382354

383355
export var DenseNumberMap = 128 | 1;
384356

385-
export var DenseSetMap = map(n1, _DSMe, 0, 0, 64 | 0);
357+
export var DenseSetMap = map(n1, _DSM, 0, 0, 64 | 0);
386358
export var DenseStringMap = 128 | 0;
387359

388-
export var DenseStructMap = map(n1, _DSMen, 0, 0, () => GreetingStruct);
360+
export var DenseStructMap = map(n1, _DSMe, 0, 0, () => GreetingStruct);
389361
export var SparseBooleanMap = map(
390362
n1,
391363
_SBM,
@@ -406,7 +378,7 @@ export var SparseNumberMap = map(
406378
);
407379
export var SparseSetMap = map(
408380
n1,
409-
_SSMp,
381+
_SSM,
410382
{
411383
[_s]: 1,
412384
},
@@ -415,7 +387,7 @@ export var SparseSetMap = map(
415387
);
416388
export var SparseStructMap = map(
417389
n1,
418-
_SSMpa,
390+
_SSMp,
419391
{
420392
[_s]: 1,
421393
},
@@ -426,7 +398,7 @@ export var TestStringMap = 128 | 0;
426398

427399
export var SparseStringMap = map(
428400
n2,
429-
_SSM,
401+
_SSMpa,
430402
{
431403
[_s]: 1,
432404
},

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ public void run() {
128128
*/
129129
private void loadShapes(Shape shape) {
130130
String absoluteName = shape.getId().toString();
131-
String name = shape.getId().getName();
132131

133132
if (shape.isMemberShape()) {
134133
loadShapes(model.expectShape(shape.asMemberShape().get().getTarget()));
@@ -139,9 +138,6 @@ private void loadShapes(Shape shape) {
139138
return;
140139
}
141140

142-
if (!elision.isReferenceSchema(shape)) {
143-
stringStore.var(name);
144-
}
145141
loadShapesVisited.add(absoluteName);
146142

147143
switch (shape.getType()) {

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@ public boolean isReferenceSchema(Shape shape) {
4646
}
4747
ShapeType type = targetShape.getType();
4848
switch (type) {
49-
case BOOLEAN, STRING, BYTE, DOUBLE, FLOAT, INTEGER, LONG, SHORT, ENUM, INT_ENUM -> {
50-
return false;
51-
}
52-
case TIMESTAMP, BLOB -> {
49+
case STRING,
50+
BOOLEAN,
51+
BYTE, DOUBLE, FLOAT, SHORT, INTEGER, LONG,
52+
ENUM, INT_ENUM,
53+
BIG_INTEGER, BIG_DECIMAL,
54+
TIMESTAMP, BLOB, DOCUMENT -> {
5355
return false;
5456
}
5557
case LIST, SET, MAP -> {

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

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
import java.util.HashSet;
1010
import java.util.Map;
1111
import java.util.Set;
12+
import java.util.TreeSet;
1213
import software.amazon.smithy.model.Model;
1314
import software.amazon.smithy.model.knowledge.KnowledgeIndex;
1415
import software.amazon.smithy.model.shapes.Shape;
1516
import software.amazon.smithy.model.shapes.ShapeId;
17+
import software.amazon.smithy.model.traits.AuthDefinitionTrait;
1618
import software.amazon.smithy.model.traits.EndpointTrait;
1719
import software.amazon.smithy.model.traits.ErrorTrait;
1820
import software.amazon.smithy.model.traits.EventHeaderTrait;
@@ -35,8 +37,8 @@
3537
import software.amazon.smithy.model.traits.SensitiveTrait;
3638
import software.amazon.smithy.model.traits.SparseTrait;
3739
import software.amazon.smithy.model.traits.StreamingTrait;
40+
import software.amazon.smithy.model.traits.TimestampFormatTrait;
3841
import software.amazon.smithy.model.traits.Trait;
39-
import software.amazon.smithy.model.traits.TraitDefinition;
4042
import software.amazon.smithy.model.traits.XmlAttributeTrait;
4143
import software.amazon.smithy.model.traits.XmlFlattenedTrait;
4244
import software.amazon.smithy.model.traits.XmlNameTrait;
@@ -46,11 +48,19 @@
4648

4749
@SmithyInternalApi
4850
final class SchemaTraitFilterIndex implements KnowledgeIndex {
51+
private static final Set<ShapeId> EXCLUDED_TRAITS = SetUtils.of(
52+
// excluded due to special schema handling.
53+
TimestampFormatTrait.ID
54+
);
55+
56+
/**
57+
* All of these are added by scanning the ProtocolDefinition and AuthDefinition meta traits.
58+
* The hard coded initial list is shown as an example of what this set contains.
59+
*/
4960
private final Set<ShapeId> includedTraits = new HashSet<>(
61+
// (wrapped for mutability)
5062
SetUtils.of(
5163
SparseTrait.ID,
52-
// excluded by special schema handling.
53-
// TimestampFormatTrait.ID,
5464
SensitiveTrait.ID,
5565
IdempotencyTokenTrait.ID,
5666
JsonNameTrait.ID,
@@ -81,15 +91,18 @@ final class SchemaTraitFilterIndex implements KnowledgeIndex {
8191
private final Model model;
8292

8393
SchemaTraitFilterIndex(Model model) {
84-
Set<Shape> shapesWithTrait = model.getShapesWithTrait(ProtocolDefinitionTrait.class);
85-
for (Shape shape : shapesWithTrait) {
86-
// todo(schema) use protocol and authDefinition traits as the initial allowlist.
87-
// System.out.println("shape having authDef: " + shape.getId().getName());
94+
Set<Shape> protocolDefinitionTraits = model.getShapesWithTrait(ProtocolDefinitionTrait.class);
95+
Set<Shape> authDefinitionTraits = model.getShapesWithTrait(AuthDefinitionTrait.class);
96+
Set<Shape> definitionTraits = new TreeSet<>();
97+
definitionTraits.addAll(protocolDefinitionTraits);
98+
definitionTraits.addAll(authDefinitionTraits);
99+
100+
for (Shape shape : definitionTraits) {
88101
shape.getTrait(ProtocolDefinitionTrait.class).ifPresent(protocolDefinitionTrait -> {
89102
protocolDefinitionTrait.getTraits().forEach(traitShapeId -> {
90-
Shape traitShape = model.expectShape(traitShapeId);
91-
TraitDefinition traitDefinition = model.getTraitDefinition(traitShapeId).get();
92-
// System.out.println("\t trait shape: " + traitShapeId.getName());
103+
if (!EXCLUDED_TRAITS.contains(traitShapeId)) {
104+
includedTraits.add(traitShapeId);
105+
}
93106
});
94107
});
95108
}
@@ -104,6 +117,14 @@ public static SchemaTraitFilterIndex of(Model model) {
104117
return model.getKnowledge(SchemaTraitFilterIndex.class, SchemaTraitFilterIndex::new);
105118
}
106119

120+
/**
121+
* @param traitShapeId - query.
122+
* @return whether trait should be included in schema generation.
123+
*/
124+
public boolean includeTrait(ShapeId traitShapeId) {
125+
return includedTraits.contains(traitShapeId) || SchemaTraitExtension.INSTANCE.contains(traitShapeId);
126+
}
127+
107128
/**
108129
* @param shape - structure or member, usually.
109130
* @return whether it has at least 1 trait that is needed in a schema.
@@ -112,7 +133,7 @@ public boolean hasSchemaTraits(Shape shape) {
112133
return hasSchemaTraits(shape, 0);
113134
}
114135

115-
public boolean hasSchemaTraits(Shape shape, int depth) {
136+
private boolean hasSchemaTraits(Shape shape, int depth) {
116137
if (cache.containsKey(shape)) {
117138
return cache.get(shape);
118139
}
@@ -139,12 +160,4 @@ public boolean hasSchemaTraits(Shape shape, int depth) {
139160
cache.put(shape, membersHaveSchemaTraits || targetHasSchemaTraits);
140161
return cache.get(shape);
141162
}
142-
143-
/**
144-
* @param traitShapeId - query.
145-
* @return whether trait should be included in schema generation.
146-
*/
147-
public boolean includeTrait(ShapeId traitShapeId) {
148-
return includedTraits.contains(traitShapeId) || SchemaTraitExtension.INSTANCE.contains(traitShapeId);
149-
}
150163
}

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,16 +98,6 @@ public String serializeTraitData(Trait trait, StringStore stringStore) {
9898
return stringStore.var(strTrait.getValue());
9999
} else if (ANNOTATION_TRAITS.contains(trait.toShapeId()) && trait instanceof AnnotationTrait) {
100100
return ANNOTATION_TRAIT_VALUE;
101-
} else if (trait instanceof HttpErrorTrait httpError) {
102-
return Objects.toString(httpError.getCode());
103-
} else if (trait instanceof HttpTrait httpTrait) {
104-
return """
105-
["%s", "%s", %s]
106-
""".formatted(
107-
httpTrait.getMethod(),
108-
httpTrait.getUri(),
109-
httpTrait.getCode()
110-
);
111101
} else if (DATA_TRAITS.contains(trait.toShapeId())) {
112102
if (trait instanceof EndpointTrait endpointTrait) {
113103
return """
@@ -120,6 +110,16 @@ public String serializeTraitData(Trait trait, StringStore stringStore) {
120110
stringStore.var(xmlNamespaceTrait.getPrefix().orElse("")),
121111
stringStore.var(xmlNamespaceTrait.getUri())
122112
);
113+
} else if (trait instanceof HttpErrorTrait httpError) {
114+
return Objects.toString(httpError.getCode());
115+
} else if (trait instanceof HttpTrait httpTrait) {
116+
return """
117+
["%s", "%s", %s]
118+
""".formatted(
119+
httpTrait.getMethod(),
120+
httpTrait.getUri(),
121+
httpTrait.getCode()
122+
);
123123
}
124124
} else if (SchemaTraitExtension.INSTANCE.contains(trait)) {
125125
return SchemaTraitExtension.INSTANCE.render(trait);

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/util/StringStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public final class StringStore {
3838
private final TreeMap<String, String> variableToLiteral = new TreeMap<>();
3939

4040
// controls incremental output.
41-
private final Set<String> writelog = new HashSet<>();
41+
private final Set<String> writeLog = new HashSet<>();
4242

4343
public StringStore() {}
4444

@@ -79,7 +79,7 @@ public String flushVariableDeclarationCode() {
7979
for (Map.Entry<String, String> entry : variableToLiteral.entrySet()) {
8080
String variable = entry.getKey();
8181
String literal = entry.getValue();
82-
if (writelog.add(variable)) {
82+
if (writeLog.add(variable)) {
8383
sourceCode.append(String.format("const %s = \"%s\";%n", variable, literal));
8484
}
8585
}

0 commit comments

Comments
 (0)