Skip to content

[http-server-csharp] add arrayDeclarationContext#10327

Draft
sophia-ramsey wants to merge 2 commits intomainfrom
sramsey/csharp-service-arrayDeclarationContext
Draft

[http-server-csharp] add arrayDeclarationContext#10327
sophia-ramsey wants to merge 2 commits intomainfrom
sramsey/csharp-service-arrayDeclarationContext

Conversation

@sophia-ramsey
Copy link
Copy Markdown
Member

In my work in the openai-openapi-pr repo, the service emitter would not compile and gave the following error message:

Error: Emit context must have a scope set in order to create references to declarations.
    at compilerAssert (asset-emitter.js)
    at invokeReference (asset-emitter.js)
    at CSharpCodeEmitter.collectionDeclaration (service.js)
    at CSharpCodeEmitter.arrayDeclaration (service.js)

I fixed the issue when I added arrayDeclarationContext to the local node_modules in my repo. Here is the fix that I made as well as a test that demonstrates the issue.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 9, 2026

All changed packages have been documented.

  • @typespec/http-server-csharp
Show changes

@typespec/http-server-csharp - fix ✏️

add arrayDeclarationContext

@azure-sdk
Copy link
Copy Markdown
Collaborator

azure-sdk commented Apr 9, 2026

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 9, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-server-csharp@10327

commit: 91d3430

@sophia-ramsey sophia-ramsey force-pushed the sramsey/csharp-service-arrayDeclarationContext branch from 215dc6d to 91d3430 Compare April 9, 2026 20:46
Copy link
Copy Markdown
Contributor

@markcowl markcowl left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! A few comments. I think the context is necessary, but the dedicated emitted file is not useful and we should avoid emitting it if possible.

`,
[
[
"Tags.cs",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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)?

"My.Custom.Ns",
],
[
["Items.cs", ["// Generated by @typespec/http-server-csharp", "using System;"]],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same comment

"public string Name { get; set; }",
],
],
["WidgetList.cs", ["// Generated by @typespec/http-server-csharp", "using System;"]],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Again, this file shouldn't exist

@route("/chat") op chat(): AssistantMessage;
`,
[
["AssistantMessage.cs", [`public string Role { get; } = "assistant";`]],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

],
],
["WidgetList.cs", ["// Generated by @typespec/http-server-csharp", "using System;"]],
["IContosoOperations.cs", ["Task<Widget[]> GetWidgetsAsync( )"]],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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

arrayDeclarationContext(array: Model, name: string, elementType: Type) {
const arrayName = ensureCSharpIdentifier(this.emitter.getProgram(), array, name);
const arrayFile = this.emitter.createSourceFile(`generated/models/${arrayName}.cs`);
arrayFile.meta[this.#sourceTypeKey] = CSharpSourceType.Model;
Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants