27
27
import java .util .HashSet ;
28
28
import java .util .List ;
29
29
import java .util .Map ;
30
+ import java .util .Objects ;
30
31
import java .util .Optional ;
31
32
import java .util .Set ;
32
33
import java .util .TreeSet ;
33
34
import java .util .function .Consumer ;
34
35
import java .util .function .Function ;
35
36
import java .util .stream .Collectors ;
36
37
import software .amazon .smithy .build .FileManifest ;
38
+ import software .amazon .smithy .codegen .core .ReservedWords ;
39
+ import software .amazon .smithy .codegen .core .ReservedWordsBuilder ;
37
40
import software .amazon .smithy .codegen .core .Symbol ;
38
41
import software .amazon .smithy .codegen .core .SymbolProvider ;
39
42
import software .amazon .smithy .model .Model ;
57
60
import software .amazon .smithy .typescript .codegen .endpointsV2 .RuleSetParameterFinder ;
58
61
import software .amazon .smithy .typescript .codegen .integration .ProtocolGenerator ;
59
62
import software .amazon .smithy .typescript .codegen .integration .RuntimeClientPlugin ;
63
+ import software .amazon .smithy .typescript .codegen .schema .SchemaGenerationAllowlist ;
60
64
import software .amazon .smithy .typescript .codegen .sections .CommandBodyExtraCodeSection ;
61
65
import software .amazon .smithy .typescript .codegen .sections .CommandConstructorCodeSection ;
62
66
import software .amazon .smithy .typescript .codegen .sections .CommandPropertiesCodeSection ;
74
78
final class CommandGenerator implements Runnable {
75
79
76
80
static final String COMMANDS_FOLDER = "commands" ;
81
+ static final String SCHEMAS_FOLDER = "schemas" ;
77
82
78
83
private final TypeScriptSettings settings ;
79
84
private final Model model ;
@@ -89,6 +94,9 @@ final class CommandGenerator implements Runnable {
89
94
private final ProtocolGenerator protocolGenerator ;
90
95
private final ApplicationProtocol applicationProtocol ;
91
96
private final SensitiveDataFinder sensitiveDataFinder ;
97
+ private final ReservedWords reservedWords = new ReservedWordsBuilder ()
98
+ .loadWords (Objects .requireNonNull (TypeScriptClientCodegenPlugin .class .getResource ("reserved-words.txt" )))
99
+ .build ();
92
100
93
101
CommandGenerator (
94
102
TypeScriptSettings settings ,
@@ -488,7 +496,10 @@ private void generateCommandMiddlewareResolver(String configType) {
488
496
);
489
497
{
490
498
// Add serialization and deserialization plugin.
491
- writer .write ("$T(config, this.serialize, this.deserialize)," , serde );
499
+ if (!SchemaGenerationAllowlist .contains (service .getId ())) {
500
+ writer .write ("$T(config, this.serialize, this.deserialize)," , serde );
501
+ }
502
+
492
503
// EndpointsV2
493
504
writer .addImport (
494
505
"getEndpointPlugin" ,
@@ -654,22 +665,37 @@ private void addCommandSpecificPlugins() {
654
665
}
655
666
}
656
667
668
+ private void writeSchemaSerde () {
669
+ String operationSchema = reservedWords .escape (operation .getId ().getName ());
670
+ writer .addRelativeImport (operationSchema , null , Paths .get (
671
+ "." , CodegenUtils .SOURCE_FOLDER , SCHEMAS_FOLDER , "schemas"
672
+ ));
673
+ writer .write ("""
674
+ .sc($L)""" ,
675
+ operationSchema
676
+ );
677
+ }
678
+
657
679
private void writeSerde () {
658
- writer
659
- .write (".ser($L)" , getSerdeDispatcher (true ))
660
- .write (".de($L)" , getSerdeDispatcher (false ));
680
+ if (SchemaGenerationAllowlist .contains (service .getId ())) {
681
+ writeSchemaSerde ();
682
+ } else {
683
+ writer
684
+ .write (".ser($L)" , getSerdeDispatcher (true ))
685
+ .write (".de($L)" , getSerdeDispatcher (false ));
686
+ }
661
687
}
662
688
663
689
private String getSerdeDispatcher (boolean isInput ) {
664
690
if (protocolGenerator == null ) {
665
691
return "() => { throw new Error(\" No supported protocol was found\" ); }" ;
666
692
} else {
667
693
String serdeFunctionName = isInput
668
- ? ProtocolGenerator .getSerFunctionShortName (symbol )
669
- : ProtocolGenerator .getDeserFunctionShortName (symbol );
694
+ ? ProtocolGenerator .getSerFunctionShortName (symbol )
695
+ : ProtocolGenerator .getDeserFunctionShortName (symbol );
670
696
writer .addRelativeImport (serdeFunctionName , null ,
671
- Paths .get ("." , CodegenUtils .SOURCE_FOLDER , ProtocolGenerator .PROTOCOLS_FOLDER ,
672
- ProtocolGenerator .getSanitizedName (protocolGenerator .getName ())));
697
+ Paths .get ("." , CodegenUtils .SOURCE_FOLDER , ProtocolGenerator .PROTOCOLS_FOLDER ,
698
+ ProtocolGenerator .getSanitizedName (protocolGenerator .getName ())));
673
699
return serdeFunctionName ;
674
700
}
675
701
}
0 commit comments