Skip to content

Commit

Permalink
Fix microsoft#5024 handling of literal types in interface ops
Browse files Browse the repository at this point in the history
  • Loading branch information
markcowl committed Nov 21, 2024
1 parent 9e33e36 commit f81be96
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/http-server-csharp/src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -685,13 +685,15 @@ export async function $onEmit(context: EmitContext<CSharpServiceEmitterOptions>)
}
let i = 1;
for (const requiredParam of requiredParams) {
const [paramType, _] = this.#findPropertyType(requiredParam);
signature.push(
code`${this.emitter.emitTypeReference(requiredParam.type)} ${ensureCSharpIdentifier(this.emitter.getProgram(), requiredParam, requiredParam.name, NameCasingType.Parameter)}${i++ < totalParams ? ", " : ""}`,
code`${paramType} ${ensureCSharpIdentifier(this.emitter.getProgram(), requiredParam, requiredParam.name, NameCasingType.Parameter)}${i++ < totalParams ? ", " : ""}`,
);
}
for (const optionalParam of optionalParams) {
const [paramType, _] = this.#findPropertyType(optionalParam);
signature.push(
code`${this.emitter.emitTypeReference(optionalParam.type)}? ${ensureCSharpIdentifier(this.emitter.getProgram(), optionalParam, optionalParam.name, NameCasingType.Parameter)}${i++ < totalParams ? ", " : ""}`,
code`${paramType}? ${ensureCSharpIdentifier(this.emitter.getProgram(), optionalParam, optionalParam.name, NameCasingType.Parameter)}${i++ < totalParams ? ", " : ""}`,
);
}
return signature.reduce();
Expand Down
51 changes: 51 additions & 0 deletions packages/http-server-csharp/test/generation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,3 +779,54 @@ it("generates appropriate types for literals", async () => {
],
);
});

it("generates appropriate types for literals in operation parameters", async () => {
await compileAndValidateMultiple(
runner,
`
/** A simple test model*/
model Foo {
/** Numeric literal */
@header intProp: 8;
/** Floating point literal */
@header floatProp: 3.14;
/** string literal */
@header stringProp: "A string of characters";
/** string template prop */
@header stringTempProp: "\${Foo.stringProp} and then some";
/** boolean */
@header trueProp: true;
/** boolean */
@header falseProp: false;
}
@route("/foo") op foo(...Foo): void;
`,
[
[
"Foo.cs",
[
"public partial class Foo",
"public int IntProp { get; } = 8",
"public double FloatProp { get; } = 3.14",
`public string StringProp { get; } = "A string of characters"`,
`public string StringTempProp { get; } = "A string of characters and then some"`,
"public bool TrueProp { get; } = true",
"public bool FalseProp { get; } = false",
],
],
[
"ContosoOperationsControllerBase.cs",
[
`public virtual async Task<IActionResult> Foo([FromHeader(Name="int-prop")] int intProp = 8, [FromHeader(Name="float-prop")] double floatProp = 3.14, [FromHeader(Name="string-prop")] string stringProp = "A string of characters", [FromHeader(Name="string-temp-prop")] string stringTempProp = "A string of characters and then some", [FromHeader(Name="true-prop")] bool trueProp = true, [FromHeader(Name="false-prop")] bool falseProp = false)`,
],
],
[
"IContosoOperations.cs",
[
`Task FooAsync( int intProp, double floatProp, string stringProp, string stringTempProp, bool trueProp, bool falseProp);`,
],
],
],
);
});

0 comments on commit f81be96

Please sign in to comment.