From f81be9610732e035fd37e880f759f7d6125cc05d Mon Sep 17 00:00:00 2001 From: Mark Cowlishaw Date: Tue, 19 Nov 2024 19:35:15 -0800 Subject: [PATCH] Fix #5024 handling of literal types in interface ops --- packages/http-server-csharp/src/service.ts | 6 ++- .../test/generation.test.ts | 51 +++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/packages/http-server-csharp/src/service.ts b/packages/http-server-csharp/src/service.ts index 5a8ede4e94..7f2d9fefa3 100644 --- a/packages/http-server-csharp/src/service.ts +++ b/packages/http-server-csharp/src/service.ts @@ -685,13 +685,15 @@ export async function $onEmit(context: EmitContext) } 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(); diff --git a/packages/http-server-csharp/test/generation.test.ts b/packages/http-server-csharp/test/generation.test.ts index b2496f8e5b..fae01567d1 100644 --- a/packages/http-server-csharp/test/generation.test.ts +++ b/packages/http-server-csharp/test/generation.test.ts @@ -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 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);`, + ], + ], + ], + ); +});