Skip to content
Draft
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
7 changes: 7 additions & 0 deletions .chronus/changes/http-specs-union-extends-2026-09-14.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/http-specs"
---

Add a round-trip scenario for unions constrained to common base types.
7 changes: 7 additions & 0 deletions .chronus/changes/spector-scenario-tspconfig-2026-09-14.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/spector"
---

Honor project `tspconfig.yaml` settings when validating scenarios and mock APIs.
79 changes: 79 additions & 0 deletions packages/http-specs/spec-summary.md
Original file line number Diff line number Diff line change
Expand Up @@ -9422,6 +9422,85 @@ Expected request to send body:
}
```

### Type_Union_Extends_Explicit_roundTrip

- Endpoint: `put /type/union/extends/explicit`

Send and receive a union whose variants explicitly extend its base type.

Expected request and response body:

```json
[
{
"name": "mittens",
"toy": "ball"
},
{
"name": "rex",
"food": "bones"
}
]
```

### Type_Union_Extends_Multiple_roundTrip

- Endpoint: `put /type/union/extends/multiple`

Send and receive the same variants through unions constrained to different base types.

Expected request and response body:

```json
{
"byName": [
{
"name": "mittens",
"food": "fish",
"toy": "ball"
},
{
"name": "rex",
"food": "bones",
"bark": true
}
],
"byFood": [
{
"name": "mittens",
"food": "fish",
"toy": "ball"
},
{
"name": "rex",
"food": "bones",
"bark": true
}
]
}
```

### Type_Union_Extends_Structural_roundTrip

- Endpoint: `put /type/union/extends/structural`

Send and receive a union whose variants structurally satisfy its base type.

Expected request and response body:

```json
[
{
"name": "mittens",
"toy": "ball"
},
{
"name": "rex",
"food": "bones"
}
]
```

### Type_Union_FloatsOnly_get

- Endpoint: `get /type/union/floats-only`
Expand Down
170 changes: 170 additions & 0 deletions packages/http-specs/specs/type/union/extends/main.tsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import "@typespec/http";
import "@typespec/spector";

using Http;
using Spector;

/**
* Describe unions constrained to a common base type.
*/
@scenarioService("/type/union/extends")
namespace Type.Union.Extends;

model StructuralPetBase {
name: string;
}

model StructuralCat {
name: string;
toy: string;
}

model StructuralDog {
name: string;
food: string;
}

union StructuralPet extends StructuralPetBase {
cat: StructuralCat,
dog: StructuralDog,
}

model ExplicitPetBase {
name: string;
}

model ExplicitCat extends ExplicitPetBase {
toy: string;
}

model ExplicitDog extends ExplicitPetBase {
food: string;
}

union ExplicitPet extends ExplicitPetBase {
cat: ExplicitCat,
dog: ExplicitDog,
}

model MultipleNameBase {
name: string;
}

model MultipleFoodBase {
food: string;
}

model MultipleCat {
name: string;
food: string;
toy: string;
}

model MultipleDog {
name: string;
food: string;
bark: boolean;
}

union MultiplePetWithName extends MultipleNameBase {
cat: MultipleCat,
dog: MultipleDog,
}

union MultiplePetWithFood extends MultipleFoodBase {
cat: MultipleCat,
dog: MultipleDog,
}
Comment on lines +69 to +77

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they have different base class


model MultipleCases {
byName: MultiplePetWithName[];
byFood: MultiplePetWithFood[];
}

@route("/structural")
interface Structural {
@scenario
@scenarioDoc("""
Send and receive a union whose variants structurally satisfy its base type.

Expected request and response body:
```json
[
{
"name": "mittens",
"toy": "ball"
},
{
"name": "rex",
"food": "bones"
}
]
```
""")
@put
roundTrip(@body input: StructuralPet[]): StructuralPet[];
}

@route("/explicit")
interface Explicit {
@scenario
@scenarioDoc("""
Send and receive a union whose variants explicitly extend its base type.

Expected request and response body:
```json
[
{
"name": "mittens",
"toy": "ball"
},
{
"name": "rex",
"food": "bones"
}
]
```
""")
@put
roundTrip(@body input: ExplicitPet[]): ExplicitPet[];
}

@route("/multiple")
interface Multiple {
@scenario
@scenarioDoc("""
Send and receive the same variants through unions constrained to different base types.

Expected request and response body:
```json
{
"byName": [
{
"name": "mittens",
"food": "fish",
"toy": "ball"
},
{
"name": "rex",
"food": "bones",
"bark": true
}
],
"byFood": [
{
"name": "mittens",
"food": "fish",
"toy": "ball"
},
{
"name": "rex",
"food": "bones",
"bark": true
}
]
}
```
""")
@put
roundTrip(@body input: MultipleCases): MultipleCases;
}
81 changes: 81 additions & 0 deletions packages/http-specs/specs/type/union/extends/mockapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import type { ScenarioMockApi } from "@typespec/spec-api";
import { json, passOnSuccess } from "@typespec/spec-api";

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

const pets = [
{
name: "mittens",
toy: "ball",
},
{
name: "rex",
food: "bones",
},
];

const multiple = {
byName: [
{
name: "mittens",
food: "fish",
toy: "ball",
},
{
name: "rex",
food: "bones",
bark: true,
},
],
byFood: [
{
name: "mittens",
food: "fish",
toy: "ball",
},
{
name: "rex",
food: "bones",
bark: true,
},
],
};

Scenarios.Type_Union_Extends_Structural_roundTrip = passOnSuccess({
uri: "/type/union/extends/structural",
method: "put",
request: {
body: json(pets),
},
response: {
status: 200,
body: json(pets),
},
kind: "MockApiDefinition",
});

Scenarios.Type_Union_Extends_Explicit_roundTrip = passOnSuccess({
uri: "/type/union/extends/explicit",
method: "put",
request: {
body: json(pets),
},
response: {
status: 200,
body: json(pets),
},
kind: "MockApiDefinition",
});

Scenarios.Type_Union_Extends_Multiple_roundTrip = passOnSuccess({
uri: "/type/union/extends/multiple",
method: "put",
request: {
body: json(multiple),
},
response: {
status: 200,
body: json(multiple),
},
kind: "MockApiDefinition",
});
3 changes: 3 additions & 0 deletions packages/http-specs/tspconfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: project
features:
- union-extends
10 changes: 9 additions & 1 deletion packages/spector/src/actions/validate-mock-apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@ export async function validateMockApis({
const diagnostics = createDiagnosticReporter();
for (const { name, specFilePath } of scenarioFiles) {
logger.debug(`Found scenario "${specFilePath}"`);
const [compilerOptions, configDiagnostics] = await specCompiler.resolveCompilerOptions(
specCompiler.NodeHost,
{
cwd: process.cwd(),
entrypoint: specFilePath,
},
);
const program = await specCompiler.compile(specCompiler.NodeHost, specFilePath, {
...compilerOptions,
noEmit: true,
warningAsError: true,
});

// Workaround https://github.com/Azure/cadl-azure/issues/2458
const programDiagnostics = program.diagnostics.filter(
const programDiagnostics = [...configDiagnostics, ...program.diagnostics].filter(
(d) =>
!(
d.code === "@azure-tools/typespec-azure-core/casing-style" &&
Expand Down
Loading
Loading