Skip to content

Commit 98ee74f

Browse files
Copilotantkmsft
andauthored
Add spector test for alternate-type with emitter fixes for external types (#861)
Co-authored-by: Anton Kolesnyk <antkmsft@users.noreply.github.com>
1 parent 84ddd38 commit 98ee74f

18 files changed

Lines changed: 972 additions & 5 deletions

File tree

packages/typespec-rust/.scripts/tspcompile.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,9 @@ function should_generate(name) {
156156
return true
157157
}
158158

159+
// When https://github.com/Azure/typespec-azure/pull/3950 is merged, and we use the newer version of @azure-tools/azure-http-specs,
160+
// we can remove alternate_types from below, add it to azureHttpSpecsGroup above, and remove the checked-in tsp files
161+
// from packages\typespec-rust\test\spector\azure\client-generator-core\alternate-type\.
159162
const alternate_types = pkgRoot + 'test/tsp/AlternateTypes';
160163
generate('alternate_types', alternate_types, 'test/other/alternate_types');
161164

@@ -192,6 +195,9 @@ generate('pub_crate', pub_crate, 'test/other/pub_crate');
192195
const client_option = pkgRoot + 'test/tsp/ClientOption';
193196
generate('client_option', client_option, 'test/other/client_option');
194197

198+
const spector_alternatetype = pkgRoot + 'test/spector/azure/client-generator-core/alternate-type/client.tsp';
199+
generate('spector_alternatetype', spector_alternatetype, 'test/spector/azure/client-generator-core/alternate-type');
200+
195201
loopSpec(httpSpecsGroup, httpSpecs)
196202
loopSpec(azureHttpSpecsGroup, azureHttpSpecs)
197203

packages/typespec-rust/src/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export async function $onEmit(context: EmitContext<RustEmitterOptions>) {
2525
let failed = false;
2626
try {
2727
const adapter = await Adapter.create(context);
28-
const crate = adapter.tcgcToCrate();
28+
const crate = adapter.tcgcToCrate(context);
2929

3030
await mkdir(`${context.emitterOutputDir}/src`, { recursive: true });
3131

packages/typespec-rust/src/tcgcadapter/adapter.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import * as utils from '../utils/utils.js';
1414
import * as tcgc from '@azure-tools/typespec-client-generator-core';
1515
import * as rust from '../codemodel/index.js';
1616
import {FinalStateValue} from "@azure-tools/typespec-azure-core";
17+
import {EmitContext, NoTarget} from "@typespec/compiler";
1718

1819
/** ErrorCode defines the types of adapter errors */
1920
export type ErrorCode =
@@ -114,8 +115,8 @@ export class Adapter {
114115
}
115116

116117
/** performs all the steps to convert tcgc to a crate */
117-
tcgcToCrate(): rust.Crate {
118-
this.adaptTypes();
118+
tcgcToCrate(context: EmitContext<RustEmitterOptions>): rust.Crate {
119+
this.adaptTypes(context);
119120
this.adaptClients();
120121

121122
// marker models don't require serde so exclude them from the check
@@ -146,10 +147,17 @@ export class Adapter {
146147
}
147148

148149
/** converts all tcgc types to their Rust type equivalent */
149-
private adaptTypes(): void {
150+
private adaptTypes(context: EmitContext<RustEmitterOptions>): void {
150151
for (const sdkUnion of this.ctx.sdkPackage.unions.filter(u => u.kind === 'union')) {
151152
if (!sdkUnion.discriminatedOptions) {
152-
throw new AdapterError('UnsupportedTsp', 'non-discriminated unions are not supported', sdkUnion.__raw?.node);
153+
// When https://github.com/microsoft/typespec/issues/9749 is fixed, we can return to throwing an exception here.
154+
context.program.reportDiagnostic({
155+
code: 'UnsupportedTsp',
156+
severity: 'warning',
157+
message: `Non-discriminated unions ('${sdkUnion.name}') are not supported. No type will be generated.`,
158+
target: NoTarget,
159+
})
160+
continue;
153161
}
154162
const rustUnion = this.getDiscriminatedUnion(sdkUnion);
155163
this.crate.unions.push(rustUnion);

0 commit comments

Comments
 (0)