-
Notifications
You must be signed in to change notification settings - Fork 389
Guard optional path parameters before formatting #11760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 4 commits
5940399
ceee147
e919a5c
bd2a68c
3068324
4795fb4
df2e7a3
baead84
e8ad7b0
c4c78a9
91c227b
be4eef7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -908,6 +908,9 @@ private void AddUriSegments( | |
| */ | ||
| var isClientParameter = ClientProvider.ClientParameters.Any(p => string.Equals(p.Name, paramName, StringComparison.OrdinalIgnoreCase)) | ||
| || _inputClient.Parameters.Any(p => p is InputMethodParameter { ParamAlias: string alias } && string.Equals(alias, paramName, StringComparison.OrdinalIgnoreCase)); | ||
| bool willEmitNullGuard = inputParamMap.TryGetValue(paramName, out var pathParamForGuard) | ||
| && pathParamForGuard.IsRequired == false | ||
| && (pathParamForGuard is InputPathParameter || pathParamForGuard is InputEndpointParameter); | ||
| CSharpType? type; | ||
| SerializationFormat? serializationFormat; | ||
| ValueExpression? valueExpression; | ||
|
|
@@ -942,19 +945,40 @@ private void AddUriSegments( | |
| } | ||
| else | ||
| { | ||
| valueExpression = type?.Equals(typeof(string)) == true | ||
| // The null check must always be performed against the raw, still-nullable value: | ||
| // for enums, GetParamInfo has already serialized valueExpression (e.g. via a | ||
| // null-conditional ToString call), so capture the check subject before any further | ||
| // unwrapping below could make it unconditionally dereference a null value. | ||
|
jorgerangel-msft marked this conversation as resolved.
Outdated
|
||
| var nullCheckExpression = valueExpression; | ||
|
jorgerangel-msft marked this conversation as resolved.
Outdated
|
||
| if (type is { IsNullable: true, IsValueType: true, IsEnum: false }) | ||
| { | ||
| valueExpression = willEmitNullGuard | ||
| ? valueExpression.Property(nameof(Nullable<int>.Value)) | ||
| : valueExpression.NullConditional(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This branch has no test coverage — including for the exact bug this PR fixes. I mutation-tested it: replacing With that mutation applied, a required-but-nullable date path parameter generates: internal PipelineMessage CreateGetThingRequest(global::System.DateTimeOffset? requestOn, RequestOptions options)
{
uri.AppendPath("/things/", false);
uri.AppendPath(requestOn.ToString("R"), true); // CS1501: DateTimeOffset? has no ToString(string)
}That is precisely issue #11759. Unmutated, the code correctly emits The gap is in Suggest adding a --generated by Copilot |
||
| } | ||
| else if (type is { IsNullable: true, IsEnum: true } && willEmitNullGuard && paramMap.TryGetValue(paramName, out var enumParamProvider)) | ||
|
jorgerangel-msft marked this conversation as resolved.
Outdated
|
||
| { | ||
| // Already inside a null guard: check the raw nullable field directly (instead of | ||
| // repeating the null-conditional serialized expression) and unwrap it via .Value | ||
| // before serializing, avoiding both a redundant guard invocation and a duplicate | ||
| // ToString call. | ||
|
jorgerangel-msft marked this conversation as resolved.
Outdated
|
||
| ValueExpression rawEnumVariable = enumParamProvider.Field is null ? enumParamProvider : enumParamProvider.Field; | ||
| nullCheckExpression = rawEnumVariable; | ||
| valueExpression = type.ToSerial(rawEnumVariable.Property(nameof(Nullable<int>.Value))); | ||
| } | ||
| valueExpression = type?.Equals(typeof(string)) == true || type?.IsEnum == true | ||
|
jorgerangel-msft marked this conversation as resolved.
Outdated
jorgerangel-msft marked this conversation as resolved.
Outdated
|
||
| ? valueExpression | ||
| : valueExpression.Invoke(nameof(ToString), toStringParams); | ||
| MethodBodyStatement statement; | ||
| if (inputParam?.IsRequired == false) | ||
| if (willEmitNullGuard) | ||
| { | ||
| bool shouldPrependWithPathSeparator = separatorDeferred || (path.Length > 0 && path[^1] != '/'); | ||
| List<MethodBodyStatement> appendPathStatements = shouldPrependWithPathSeparator | ||
| ? [uri.AppendPath(Literal("/"), false).Terminate(), uri.AppendPath(valueExpression, escape).Terminate()] | ||
| : [uri.AppendPath(valueExpression, escape).Terminate()]; | ||
| statement = BuildQueryOrHeaderOrPathParameterNullCheck( | ||
| type, | ||
| valueExpression, | ||
| nullCheckExpression, | ||
| appendPathStatements); | ||
| } | ||
| else | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // <auto-generated/> | ||
|
|
||
| #nullable disable | ||
|
|
||
| using System; | ||
| using System.ClientModel.Primitives; | ||
|
|
||
| namespace Sample | ||
| { | ||
| public partial class TestClient | ||
| { | ||
| private static global::System.ClientModel.Primitives.PipelineMessageClassifier _pipelineMessageClassifier200; | ||
|
|
||
| private static global::System.ClientModel.Primitives.PipelineMessageClassifier PipelineMessageClassifier200 => _pipelineMessageClassifier200 ??= global::System.ClientModel.Primitives.PipelineMessageClassifier.Create(stackalloc ushort[] { 200 }); | ||
|
|
||
| internal global::System.ClientModel.Primitives.PipelineMessage CreateGetThingRequest(global::System.DateTimeOffset? requestOn, global::System.ClientModel.Primitives.RequestOptions options) | ||
| { | ||
| global::Sample.ClientUriBuilder uri = new global::Sample.ClientUriBuilder(); | ||
| uri.Reset(_endpoint); | ||
| uri.AppendPath("/things", false); | ||
| if ((requestOn != null)) | ||
| { | ||
| uri.AppendPath("/", false); | ||
| uri.AppendPath(requestOn.Value.ToString("R"), true); | ||
| } | ||
| global::System.ClientModel.Primitives.PipelineMessage message = Pipeline.CreateMessage(uri.ToUri(), "GET", PipelineMessageClassifier200); | ||
| global::System.ClientModel.Primitives.PipelineRequest request = message.Request; | ||
| message.Apply(options); | ||
| return message; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // <auto-generated/> | ||
|
|
||
| #nullable disable | ||
|
|
||
| using System; | ||
| using System.ClientModel.Primitives; | ||
|
|
||
| namespace Sample | ||
| { | ||
| public partial class TestClient | ||
| { | ||
| private static global::System.ClientModel.Primitives.PipelineMessageClassifier _pipelineMessageClassifier200; | ||
|
|
||
| private static global::System.ClientModel.Primitives.PipelineMessageClassifier PipelineMessageClassifier200 => _pipelineMessageClassifier200 ??= global::System.ClientModel.Primitives.PipelineMessageClassifier.Create(stackalloc ushort[] { 200 }); | ||
|
|
||
| internal global::System.ClientModel.Primitives.PipelineMessage CreateGetThingRequest(global::System.DateTimeOffset requestOn, global::System.ClientModel.Primitives.RequestOptions options) | ||
| { | ||
| global::Sample.ClientUriBuilder uri = new global::Sample.ClientUriBuilder(); | ||
| uri.Reset(_endpoint); | ||
| uri.AppendPath("/things/", false); | ||
| uri.AppendPath(requestOn.ToString("R"), true); | ||
| global::System.ClientModel.Primitives.PipelineMessage message = Pipeline.CreateMessage(uri.ToUri(), "GET", PipelineMessageClassifier200); | ||
| global::System.ClientModel.Primitives.PipelineRequest request = message.Request; | ||
| message.Apply(options); | ||
| return message; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| // <auto-generated/> | ||
|
|
||
| #nullable disable | ||
|
|
||
| using System.ClientModel.Primitives; | ||
|
|
||
| namespace Sample | ||
| { | ||
| public partial class TestClient | ||
| { | ||
| private static global::System.ClientModel.Primitives.PipelineMessageClassifier _pipelineMessageClassifier200; | ||
|
|
||
| private static global::System.ClientModel.Primitives.PipelineMessageClassifier PipelineMessageClassifier200 => _pipelineMessageClassifier200 ??= global::System.ClientModel.Primitives.PipelineMessageClassifier.Create(stackalloc ushort[] { 200 }); | ||
|
|
||
| internal global::System.ClientModel.Primitives.PipelineMessage CreateGetThingRequest(global::System.ClientModel.Primitives.RequestOptions options) | ||
| { | ||
| global::Sample.ClientUriBuilder uri = new global::Sample.ClientUriBuilder(); | ||
| uri.Reset(_endpoint); | ||
| uri.AppendPath("/things", false); | ||
| if ((_color != null)) | ||
| { | ||
| uri.AppendPath("/", false); | ||
| uri.AppendPath(_color.Value.ToString(), true); | ||
| } | ||
| global::System.ClientModel.Primitives.PipelineMessage message = Pipeline.CreateMessage(uri.ToUri(), "GET", PipelineMessageClassifier200); | ||
| global::System.ClientModel.Primitives.PipelineRequest request = message.Request; | ||
| message.Apply(options); | ||
| return message; | ||
| } | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.