-
Notifications
You must be signed in to change notification settings - Fork 354
[http-server-csharp] add arrayDeclarationContext #10327
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| changeKind: fix | ||
| packages: | ||
| - "@typespec/http-server-csharp" | ||
| --- | ||
|
|
||
| add arrayDeclarationContext |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3396,6 +3396,109 @@ describe("collection type: defined as emitter option", () => { | |
| }); | ||
| }); | ||
|
|
||
| describe("arrayDeclarationContext", () => { | ||
| it("generates a dedicated file for array model declarations", async () => { | ||
| await compileAndValidateMultiple( | ||
| tester, | ||
| ` | ||
| model Tags is Array<string>; | ||
| @route("/tags") @get op getTags(): Tags; | ||
| `, | ||
| [ | ||
| [ | ||
| "Tags.cs", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we generating a file for this? What do we expect the file to contain? Shouldn't we find a way to not emit the array type (while making sure we emit the item type)? |
||
| [ | ||
| "// Generated by @typespec/http-server-csharp", | ||
| "using System;", | ||
| "using System.Text.Json;", | ||
| "using System.Text.Json.Serialization;", | ||
| "using TypeSpec.Helpers.JsonConverters;", | ||
| "using TypeSpec.Helpers;", | ||
| ], | ||
| ], | ||
| ["IContosoOperations.cs", ["Task<string[]> GetTagsAsync( )"]], | ||
| ], | ||
| ); | ||
| }); | ||
|
|
||
| it("generates a dedicated file for array model with custom namespace", async () => { | ||
| await compileAndValidateMultiple( | ||
| tester, | ||
| [ | ||
| ` | ||
| model Items is Array<int32>; | ||
| @route("/items") @get op getItems(): Items; | ||
| `, | ||
| "My.Custom.Ns", | ||
| ], | ||
| [ | ||
| ["Items.cs", ["// Generated by @typespec/http-server-csharp", "using System;"]], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment |
||
| ["INsOperations.cs", ["Task<int[]> GetItemsAsync( )"]], | ||
| ], | ||
| ); | ||
| }); | ||
|
|
||
| it("generates a dedicated file for array model with complex element type", async () => { | ||
| await compileAndValidateMultiple( | ||
| tester, | ||
| ` | ||
| model Widget { | ||
| id: int32; | ||
| name: string; | ||
| } | ||
| model WidgetList is Array<Widget>; | ||
| @route("/widgets") @get op getWidgets(): WidgetList; | ||
| `, | ||
| [ | ||
| [ | ||
| "Widget.cs", | ||
| [ | ||
| "public partial class Widget", | ||
| "public int Id { get; set; }", | ||
| "public string Name { get; set; }", | ||
| ], | ||
| ], | ||
| ["WidgetList.cs", ["// Generated by @typespec/http-server-csharp", "using System;"]], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, this file shouldn't exist |
||
| ["IContosoOperations.cs", ["Task<Widget[]> GetWidgetsAsync( )"]], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we should make sure there is an appropriate using for the namespace containing Widget |
||
| ], | ||
| ); | ||
| }); | ||
|
|
||
| it("generates a dedicated file for named array with union element type", async () => { | ||
| await compileAndValidateMultiple( | ||
| tester, | ||
| ` | ||
| model ToolCall { | ||
| id: string; | ||
| type: "function"; | ||
| name: string; | ||
| } | ||
|
|
||
| model CustomToolCall { | ||
| id: string; | ||
| type: "custom"; | ||
| payload: string; | ||
| } | ||
|
|
||
| /** The tool calls generated by the model, such as function calls. */ | ||
| model ToolCalls is (ToolCall | CustomToolCall)[]; | ||
|
|
||
| model AssistantMessage { | ||
| role: "assistant"; | ||
| tool_calls?: ToolCalls; | ||
| } | ||
|
|
||
| @route("/chat") op chat(): AssistantMessage; | ||
| `, | ||
| [ | ||
| ["AssistantMessage.cs", [`public string Role { get; } = "assistant";`]], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like to test for the rest of the defintion here (I think it should either be an Object or a generic json type |
||
| ["ToolCalls.cs", ["// Generated by @typespec/http-server-csharp"]], | ||
| ["IContosoOperations.cs", ["Task<AssistantMessage> ChatAsync( );"]], | ||
| ], | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it("emits class for model extending another model with no additional properties", async () => { | ||
| await compileAndValidateMultiple( | ||
| tester, | ||
|
|
||
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.
We could potentially use this to not emit the useless array model. Ideally, we could create a context without an emitted file.