Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/azure-http-specs"
---

Add common package reference test for `@alternateType` with `ExternalType`. This test demonstrates using `@alternateType` to reference types from a pre-existing typespec-defined common package, where each language emitter specifies its own generated package coordinates. Includes a models-only `common-types` spector package defining an `Address` model.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import "@azure-tools/typespec-client-generator-core";
import "./main.tsp";

using Azure.ClientGenerator.Core;

// Python: reference from the generated common-types package
@@alternateType(_Specs_.Azure.ClientGenerator.Core.AlternateType.CommonPackageRef.Address,
{
identity: "common_types.models.Address",
package: "common-types",
minVersion: "1.0.0",
},
"python"
);

// Java: reference from the generated common-types package
@@alternateType(_Specs_.Azure.ClientGenerator.Core.AlternateType.CommonPackageRef.Address,
{
identity: "com.azure.common.types.models.Address",
package: "com.azure.common-types",
minVersion: "1.0.0",
},
"java"
);

// C#: reference from the generated common-types package
@@alternateType(_Specs_.Azure.ClientGenerator.Core.AlternateType.CommonPackageRef.Address,
{
identity: "CommonTypes.Models.Address",
package: "CommonTypes",
minVersion: "1.0.0",
},
"csharp"
);

// TypeScript: reference from the generated common-types package
@@alternateType(_Specs_.Azure.ClientGenerator.Core.AlternateType.CommonPackageRef.Address,
{
identity: "Address",
package: "common-types",
minVersion: "1.0.0",
},
"typescript"
);

// Go: reference from the generated common-types package
@@alternateType(_Specs_.Azure.ClientGenerator.Core.AlternateType.CommonPackageRef.Address,
{
identity: "commontypes.Address",
package: "github.com/Azure/azure-sdk-for-go/sdk/common-types",
minVersion: "1.0.0",
},
"go"
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import "@typespec/http";
import "@typespec/spector";

using Http;
using Spector;

/**
* A models-only TypeSpec package representing pre-generated common/shared types.
*
* Language emitters should generate this package independently, then services
* that use `@alternateType` with `ExternalType` can reference the generated
* package instead of re-generating these types inline.
*/
@scenarioService("/azure/client-generator-core/alternate-type/common-package-ref/common-types")
@service
namespace _Specs_.Azure.ClientGenerator.Core.AlternateType.CommonPackageRef.CommonTypes;

/** A postal address. */
model Address {
city: string;
state: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { ScenarioMockApi } from "@typespec/spec-api";

// Models-only package — no operations, no scenarios.
export const Scenarios: Record<string, ScenarioMockApi> = {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import "@typespec/http";
import "@typespec/spector";
import "@azure-tools/typespec-client-generator-core";

using Http;
using Azure.ClientGenerator.Core;
using Spector;

@doc("""
Test using @alternateType with ExternalType to reference a pre-existing
typespec-defined common package. Instead of generating types inline,
language emitters should reference the types from the common package.
""")
@scenarioService("/azure/client-generator-core/alternate-type/common-package-ref")
@service
namespace _Specs_.Azure.ClientGenerator.Core.AlternateType.CommonPackageRef;

/** Address model that mirrors the common package's Address type on the wire. */
model Address {
city: string;
state: string;
}

/** Model that has a property referencing the common package type. */
model ModelWithAddressProperty {
address: Address;
name: string;
}

@scenario
@scenarioDoc("""
Input: None
Output: Address object.
Example response:
```json
{
"city": "Seattle",
"state": "WA"
}
```
""")
@route("/model")
@get
op getModel(): Address;

@scenario
@scenarioDoc("""
Input: Address object in request body.
Example input:
```json
{
"city": "Seattle",
"state": "WA"
}
```
Output: None (204/empty response)
""")
@route("/model")
@put
op putModel(@body body: Address): void;

@scenario
@scenarioDoc("""
Input: None
Output: ModelWithAddressProperty object.
Example response:
```json
{
"address": {
"city": "Seattle",
"state": "WA"
},
"name": "test"
}
```
""")
@route("/property")
@get
op getProperty(): ModelWithAddressProperty;

@scenario
@scenarioDoc("""
Input: ModelWithAddressProperty object in request body.
Example input:
```json
{
"address": {
"city": "Seattle",
"state": "WA"
},
"name": "test"
}
```
Output: None (204/empty response)
""")
@route("/property")
@put
op putProperty(@body body: ModelWithAddressProperty): void;
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
json,
MockApiDefinition,
MockBody,
passOnSuccess,
ScenarioMockApi,
} from "@typespec/spec-api";

export const Scenarios: Record<string, ScenarioMockApi> = {};

function createMockApiDefinitions(
route: string,
body: MockBody,
): [MockApiDefinition, MockApiDefinition] {
return [
{
uri: `/azure/client-generator-core/alternate-type/common-package-ref/${route}`,
method: "get",
response: {
status: 200,
body: body,
},
kind: "MockApiDefinition",
},
{
uri: `/azure/client-generator-core/alternate-type/common-package-ref/${route}`,
method: "put",
request: {
body,
},
response: { status: 204 },
kind: "MockApiDefinition",
},
];
}

const address = {
city: "Seattle",
state: "WA",
};

const modelScenarioTypes = createMockApiDefinitions("model", json(address));

Scenarios.Azure_ClientGenerator_Core_AlternateType_CommonPackageRef_getModel = passOnSuccess(
modelScenarioTypes[0],
);
Scenarios.Azure_ClientGenerator_Core_AlternateType_CommonPackageRef_putModel = passOnSuccess(
modelScenarioTypes[1],
);

const modelWithAddressProperty = {
address,
name: "test",
};

const modelPropertyScenarioTypes = createMockApiDefinitions(
"property",
json(modelWithAddressProperty),
);

Scenarios.Azure_ClientGenerator_Core_AlternateType_CommonPackageRef_getProperty = passOnSuccess(
modelPropertyScenarioTypes[0],
);
Scenarios.Azure_ClientGenerator_Core_AlternateType_CommonPackageRef_putProperty = passOnSuccess(
modelPropertyScenarioTypes[1],
);
Loading