-
Notifications
You must be signed in to change notification settings - Fork 394
Add union extends HTTP spec scenarios #11957
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Weidong Xu (weidongxu-microsoft)
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
weidongxu-microsoft:feature/http-spec-union-extends
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
89bd421
feat(http-specs): add union extends scenario
weidongxu-microsoft 7dbd0f7
test(http-specs): split union extends cases
weidongxu-microsoft 70d0d7b
test(http-specs): flatten union extends models
weidongxu-microsoft f1a25dc
style(http-specs): format union extends scenarios
weidongxu-microsoft File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| } | ||
|
|
||
| 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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| kind: project | ||
| features: | ||
| - union-extends |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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