Skip to content

Sample Generation - ExampleValueExpressionBuilder and OperationSample parity with autorest generator#10739

Open
MaiLinhP wants to merge 15 commits into
microsoft:mainfrom
MaiLinhP:sample
Open

Sample Generation - ExampleValueExpressionBuilder and OperationSample parity with autorest generator#10739
MaiLinhP wants to merge 15 commits into
microsoft:mainfrom
MaiLinhP:sample

Conversation

@MaiLinhP
Copy link
Copy Markdown
Member

This PR implements Milestones 2 and 3 of the sample/test generation design for the TypeSpec C# generator, adding the core infrastructure to convert example values into C# expressions and to model operation samples with client chain resolution and parameter mapping.

Milestone 2 — Example Value → C# Expression Converter

ExampleValueExpressionBuilder — converts InputExampleValue + CSharpType into ValueExpression AST nodes. Handles all framework primitives (string, int, long, float, double, decimal, bool, Guid, Uri, byte[]), DateTimeOffset (format-aware: Parse vs FromUnixTimeSeconds), TimeSpan (format-aware: XmlConvert.ToTimeSpan vs FromSeconds), BinaryData, Stream, lists, dictionaries, models (with constructor parameter matching), and enums.
ExampleParameterValue — dual-mode bridge type: carries either a raw InputExampleValue (for spec-provided params) or a pre-built ValueExpression

Milestone 3 — Operation Sample Model

OperationSample — bridges InputOperationExample to everything needed for code generation: client invocation chain (walks subclient → root, collecting factory methods + constructor), parameter value mapping with known-parameter handling (endpoint, API key, token credential → DefaultAzureCredential, WaitUntil, RequestContent, MatchConditions, CancellationToken), inline vs out-of-line decision, and static helpers (ShouldGenerateSample, ShouldGenerateShortVersion).
ScmMethodProviderCollection.BuildSamples() — attaches protocol and convenience OperationSample instances to each method collection, respecting ShortVersion suppression logic.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label May 19, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 19, 2026

Open in StackBlitz

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

commit: 2350208

@github-actions
Copy link
Copy Markdown
Contributor

No changes needing a change description found.

@MaiLinhP MaiLinhP marked this pull request as ready for review May 21, 2026 17:29
Copy link
Copy Markdown
Contributor

@jorgerangel-msft jorgerangel-msft left a comment

Choose a reason for hiding this comment

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

Sorry if I missed this, but do you have a design doc of the proposed sample generation pipeline? I think that will help with understanding where these changes fit in the overall picture.

new ParameterProvider("nextPage", $"The url of the next page of responses.", typeof(Uri));

public static readonly ParameterProvider KeyCredential = new("credential", FormattableStringHelpers.Empty, typeof(ApiKeyCredential));
public static readonly ParameterProvider TokenCredential = new("credential", FormattableStringHelpers.Empty, typeof(AuthenticationTokenProvider));
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.

should we use the ClientPipelineApi abstraction for these two parameters?

];
}

protected virtual IReadOnlyList<OperationSample> BuildSamples()
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.

Is there a reason why this is protected virtual? Is it for testing? Also do you think the sample building should be decoupled from the client method construction we do here?

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

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants