Skip to content

Commit 08e3b88

Browse files
authored
fix(codegen): handle shape name collisions for exceptions and Unit (#1763)
1 parent 8aef84b commit 08e3b88

File tree

14 files changed

+107
-22
lines changed

14 files changed

+107
-22
lines changed

private/my-local-model/src/commands/GetNumbersCommand.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export interface GetNumbersCommandOutput extends GetNumbersResponse, __MetadataB
6666
*
6767
* @throws {@link HaltError} (client fault)
6868
*
69-
* @throws {@link XYZServiceServiceException}
69+
* @throws {@link XYZServiceServiceException} (client fault)
70+
*
71+
* @throws {@link XYZServiceSyntheticServiceException}
7072
* <p>Base exception class for all service exceptions from XYZService service.</p>
7173
*
7274
*

private/my-local-model/src/commands/TradeEventStreamCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface TradeEventStreamCommandOutput extends TradeEventStreamResponse,
7373
* @see {@link TradeEventStreamCommandOutput} for command's `response` shape.
7474
* @see {@link XYZServiceClientResolvedConfig | config} for XYZServiceClient's `config` shape.
7575
*
76-
* @throws {@link XYZServiceServiceException}
76+
* @throws {@link XYZServiceSyntheticServiceException}
7777
* <p>Base exception class for all service exceptions from XYZService service.</p>
7878
*
7979
*

private/my-local-model/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ export type { XYZServiceExtensionConfiguration } from "./extensionConfiguration"
1313
export * from "./commands";
1414
export * from "./models";
1515

16-
export { XYZServiceServiceException } from "./models/XYZServiceServiceException";
16+
export { XYZServiceSyntheticServiceException } from "./models/XYZServiceSyntheticServiceException";

private/my-local-model/src/models/XYZServiceServiceException.ts renamed to private/my-local-model/src/models/XYZServiceSyntheticServiceException.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ export { __ServiceException };
1313
*
1414
* Base exception class for all service exceptions from XYZService service.
1515
*/
16-
export class XYZServiceServiceException extends __ServiceException {
16+
export class XYZServiceSyntheticServiceException extends __ServiceException {
1717
/**
1818
* @internal
1919
*/
2020
constructor(options: __ServiceExceptionOptions) {
2121
super(options);
22-
Object.setPrototypeOf(this, XYZServiceServiceException.prototype);
22+
Object.setPrototypeOf(this, XYZServiceSyntheticServiceException.prototype);
2323
}
2424
}

private/my-local-model/src/models/models_0.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// smithy-typescript generated code
2-
import { XYZServiceServiceException as __BaseException } from "./XYZServiceServiceException";
2+
import { XYZServiceSyntheticServiceException as __BaseException } from "./XYZServiceSyntheticServiceException";
33
import { NumericValue } from "@smithy/core/serde";
44
import { ExceptionOptionType as __ExceptionOptionType } from "@smithy/smithy-client";
55

@@ -125,6 +125,25 @@ export class RetryableError extends __BaseException {
125125
}
126126
}
127127

128+
/**
129+
* @public
130+
*/
131+
export class XYZServiceServiceException extends __BaseException {
132+
readonly name: "XYZServiceServiceException" = "XYZServiceServiceException";
133+
readonly $fault: "client" = "client";
134+
/**
135+
* @internal
136+
*/
137+
constructor(opts: __ExceptionOptionType<XYZServiceServiceException, __BaseException>) {
138+
super({
139+
name: "XYZServiceServiceException",
140+
$fault: "client",
141+
...opts,
142+
});
143+
Object.setPrototypeOf(this, XYZServiceServiceException.prototype);
144+
}
145+
}
146+
128147
/**
129148
* @public
130149
*/

private/my-local-model/src/protocols/Rpcv2cbor.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// smithy-typescript generated code
22
import { GetNumbersCommandInput, GetNumbersCommandOutput } from "../commands/GetNumbersCommand";
33
import { TradeEventStreamCommandInput, TradeEventStreamCommandOutput } from "../commands/TradeEventStreamCommand";
4-
import { XYZServiceServiceException as __BaseException } from "../models/XYZServiceServiceException";
4+
import { XYZServiceSyntheticServiceException as __BaseException } from "../models/XYZServiceSyntheticServiceException";
55
import {
66
Alpha,
77
CodedThrottlingError,
@@ -12,6 +12,7 @@ import {
1212
RetryableError,
1313
TradeEvents,
1414
Unit,
15+
XYZServiceServiceException,
1516
} from "../models/models_0";
1617
import {
1718
dateToTag as __dateToTag,
@@ -138,6 +139,9 @@ const de_CommandError = async (output: __HttpResponse, context: __SerdeContext):
138139
case "RetryableError":
139140
case "org.xyz.v1#RetryableError":
140141
throw await de_RetryableErrorRes(parsedOutput, context);
142+
case "XYZServiceServiceException":
143+
case "org.xyz.v1#XYZServiceServiceException":
144+
throw await de_XYZServiceServiceExceptionRes(parsedOutput, context);
141145
default:
142146
const parsedBody = parsedOutput.body;
143147
return throwDefaultError({
@@ -206,6 +210,22 @@ const de_RetryableErrorRes = async (parsedOutput: any, context: __SerdeContext):
206210
return __decorateServiceException(exception, body);
207211
};
208212

213+
/**
214+
* deserializeRpcv2cborXYZServiceServiceExceptionRes
215+
*/
216+
const de_XYZServiceServiceExceptionRes = async (
217+
parsedOutput: any,
218+
context: __SerdeContext
219+
): Promise<XYZServiceServiceException> => {
220+
const body = parsedOutput.body;
221+
const deserialized: any = _json(body);
222+
const exception = new XYZServiceServiceException({
223+
$metadata: deserializeMetadata(parsedOutput),
224+
...deserialized,
225+
});
226+
return __decorateServiceException(exception, body);
227+
};
228+
209229
/**
210230
* serializeRpcv2cborTradeEvents
211231
*/
@@ -331,6 +351,8 @@ const de_GetNumbersResponse = (output: any, context: __SerdeContext): GetNumbers
331351

332352
// de_RetryableError omitted.
333353

354+
// de_XYZServiceServiceException omitted.
355+
334356
// de_Unit omitted.
335357

336358
const deserializeMetadata = (output: __HttpResponse): __ResponseMetadata => ({

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ import {
148148

149149
/* eslint no-var: 0 */
150150

151-
export var Unit = "unit" as const;
151+
export var __Unit = "unit" as const;
152152

153153
export var ValidationException: StaticErrorSchema = [
154154
-3,
@@ -435,10 +435,10 @@ export var SparseStringMap: StaticMapSchema = [
435435
0,
436436
];
437437
export var EmptyInputOutput: StaticOperationSchema = [9, n1, _EIO, 0, () => EmptyStructure, () => EmptyStructure];
438-
export var Float16: StaticOperationSchema = [9, n1, _Fl, 0, () => Unit, () => Float16Output];
439-
export var FractionalSeconds: StaticOperationSchema = [9, n1, _FS, 0, () => Unit, () => FractionalSecondsOutput];
440-
export var GreetingWithErrors: StaticOperationSchema = [9, n1, _GWE, 2, () => Unit, () => GreetingWithErrorsOutput];
441-
export var NoInputOutput: StaticOperationSchema = [9, n1, _NIO, 0, () => Unit, () => Unit];
438+
export var Float16: StaticOperationSchema = [9, n1, _Fl, 0, () => __Unit, () => Float16Output];
439+
export var FractionalSeconds: StaticOperationSchema = [9, n1, _FS, 0, () => __Unit, () => FractionalSecondsOutput];
440+
export var GreetingWithErrors: StaticOperationSchema = [9, n1, _GWE, 2, () => __Unit, () => GreetingWithErrorsOutput];
441+
export var NoInputOutput: StaticOperationSchema = [9, n1, _NIO, 0, () => __Unit, () => __Unit];
442442
export var OperationWithDefaults: StaticOperationSchema = [
443443
9,
444444
n1,

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CodegenUtils.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,36 @@ public static String getServiceName(
352352
return symbolProvider.toSymbol(service).getName().replaceAll("(Client)$", "");
353353
}
354354

355+
/**
356+
* The alternative should be used because the base exception may have been renamed
357+
* due to a collision in the model.
358+
*
359+
* @deprecated use {@link #getSyntheticBaseExceptionName}.
360+
*/
361+
@Deprecated
355362
public static String getServiceExceptionName(String serviceName) {
356363
return serviceName + "ServiceException";
357364
}
365+
366+
/**
367+
* If the service unfortunately defines a ServiceNameBaseException error,
368+
* we still generate a synthetic base error for uniformity, but it must be renamed.
369+
*/
370+
public static String getSyntheticBaseExceptionName(String serviceName, Model model) {
371+
String serviceExceptionName = getServiceExceptionName(serviceName);
372+
while (true) {
373+
String finalServiceExceptionName = serviceExceptionName;
374+
boolean namingCollision = model.getStructureShapes()
375+
.stream()
376+
.anyMatch(structureShape -> structureShape.getId().getName().equals(finalServiceExceptionName));
377+
378+
if (namingCollision) {
379+
serviceExceptionName = serviceExceptionName
380+
.replaceAll("ServiceException$", "SyntheticServiceException");
381+
} else {
382+
break;
383+
}
384+
}
385+
return serviceExceptionName;
386+
}
358387
}

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/CommandGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ private String getThrownExceptions() {
396396
}
397397

398398
String name = CodegenUtils.getServiceName(settings, model, symbolProvider);
399-
buffer.append(String.format("@throws {@link %s}%n", CodegenUtils.getServiceExceptionName(name)));
399+
buffer.append(String.format("@throws {@link %s}%n", CodegenUtils.getSyntheticBaseExceptionName(name, model)));
400400
buffer.append(String.format("<p>Base exception class for all service exceptions from %s service.</p>%n", name));
401401

402402
return buffer.toString();

smithy-typescript-codegen/src/main/java/software/amazon/smithy/typescript/codegen/integration/AddBaseServiceExceptionClass.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private void writeAdditionalFiles(
5959
boolean isClientSdk = settings.generateClient();
6060
if (isClientSdk) {
6161
String serviceName = CodegenUtils.getServiceName(settings, model, symbolProvider);
62-
String serviceExceptionName = CodegenUtils.getServiceExceptionName(serviceName);
62+
String serviceExceptionName = CodegenUtils.getSyntheticBaseExceptionName(serviceName, model);
6363
writerFactory.accept(
6464
Paths.get(CodegenUtils.SOURCE_FOLDER, "models", serviceExceptionName + ".ts").toString(),
6565
writer -> {
@@ -93,7 +93,7 @@ private void writeAdditionalExports(
9393
boolean isClientSdk = settings.generateClient();
9494
if (isClientSdk) {
9595
String serviceName = CodegenUtils.getServiceName(settings, model, symbolProvider);
96-
String serviceExceptionName = CodegenUtils.getServiceExceptionName(serviceName);
96+
String serviceExceptionName = CodegenUtils.getSyntheticBaseExceptionName(serviceName, model);
9797
writer.write("export { $1L } from \"./models/$1L\";", serviceExceptionName);
9898
}
9999
}
@@ -117,7 +117,7 @@ public SymbolProvider decorateSymbolProvider(
117117
String baseExceptionAlias = "__BaseException";
118118
SymbolReference reference;
119119
if (settings.generateClient()) {
120-
String serviceExceptionName = CodegenUtils.getServiceExceptionName(serviceName);
120+
String serviceExceptionName = CodegenUtils.getSyntheticBaseExceptionName(serviceName, model);
121121
String namespace = Paths.get(".", "src", "models", serviceExceptionName).toString();
122122
Symbol serviceExceptionSymbol = Symbol.builder()
123123
.name(serviceExceptionName)
@@ -140,5 +140,4 @@ public SymbolProvider decorateSymbolProvider(
140140
return symbol;
141141
};
142142
}
143-
144143
}

0 commit comments

Comments
 (0)