typespec icon indicating copy to clipboard operation
typespec copied to clipboard

Add expression converter for sample generation

Open radhgupta opened this issue 7 months ago • 3 comments

What

Adds ExampleValueExpressionBuilder — converts InputExampleValue + CSharpType → ValueExpression (C# AST nodes). Also adds ExampleParameterValue bridge type. This is M2 of the test/sample generation feature.

How it works

The builder dispatches on CSharpType to produce the correct C# expression:

Input Output
"hello" + string Literal("hello")
"73f4..." + Guid Guid.Parse("73f4...")
"http://..." + Uri new Uri("http://...")
1234 + int Literal(1234)
{...} + BinaryData BinaryData.FromObjectAsJson(new { ... })
[...] + IList<T> new T[] { ... }
"Monday" + DayOfWeek DayOfWeek.Monday

ExampleParameterValue supports two modes — pre-built Expression (for known params like credentials) or raw Value (for spec-provided data needing conversion).

Changes

  • New: ClientModel/src/Providers/Samples/ExampleValueExpressionBuilder.cs
  • New: ClientModel/src/Providers/Samples/ExampleParameterValue.cs
  • New: ClientModel/test/Providers/Samples/ExampleValueExpressionBuilderTests.cs (52 tests)
  • Modified: Input/src/Properties/AssemblyInfo.cs — added InternalsVisibleTo for Generator and ClientModel projects to access internal example subtypes (InputExampleRawValue, etc.)

Design doc

https://gist.github.com/radhgupta/9fee69190f1ac2252a88dd88f99de327

radhgupta avatar Apr 15 '26 21:04 radhgupta

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@10393

commit: d42e53a

pkg-pr-new[bot] avatar Apr 15 '26 21:04 pkg-pr-new[bot]

No changes needing a change description found.

github-actions[bot] avatar Apr 15 '26 21:04 github-actions[bot]

Hi @@radhgupta. Your PR has had no update for 30 days and it is marked as a stale PR. If it is not updated within 30 days, the PR will automatically be closed. If you want to refresh the PR, please remove the stale label.

Hi @@radhgupta. The PR will be closed since the PR has no update for 28 days. If this is still relevant please reopen.