Skip to content

feat: Add StringSyntaxAttribute for guid and timespan args #48662

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Components/Components/src/BindConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ private static bool ConvertToDateTimeCore(object? obj, CultureInfo? culture, out
return ConvertToDateTimeCore(obj, culture, format: null, out value);
}

private static bool ConvertToDateTimeCore(object? obj, CultureInfo? culture, string? format, out DateTime value)
private static bool ConvertToDateTimeCore(object? obj, CultureInfo? culture, [StringSyntax(StringSyntaxAttribute.TimeSpanFormat)] string? format, out DateTime value)
{
var text = (string?)obj;
if (string.IsNullOrEmpty(text))
Expand Down Expand Up @@ -1252,7 +1252,7 @@ private static bool ConvertToNullableDateTimeCore(object? obj, CultureInfo? cult
return ConvertToNullableDateTimeCore(obj, culture, format: null, out value);
}

private static bool ConvertToNullableDateTimeCore(object? obj, CultureInfo? culture, string? format, out DateTime? value)
private static bool ConvertToNullableDateTimeCore(object? obj, CultureInfo? culture, [StringSyntax(StringSyntaxAttribute.DateTimeFormat)] string? format, out DateTime? value)
{
var text = (string?)obj;
if (string.IsNullOrEmpty(text))
Expand Down Expand Up @@ -1336,7 +1336,7 @@ private static bool ConvertToDateTimeOffsetCore(object? obj, CultureInfo? cultur
return ConvertToDateTimeOffsetCore(obj, culture, format: null, out value);
}

private static bool ConvertToDateTimeOffsetCore(object? obj, CultureInfo? culture, string? format, out DateTimeOffset value)
private static bool ConvertToDateTimeOffsetCore(object? obj, CultureInfo? culture, [StringSyntax(StringSyntaxAttribute.DateTimeFormat)] string? format, out DateTimeOffset value)
{
var text = (string?)obj;
if (string.IsNullOrEmpty(text))
Expand Down Expand Up @@ -1365,7 +1365,7 @@ private static bool ConvertToNullableDateTimeOffsetCore(object? obj, CultureInfo
return ConvertToNullableDateTimeOffsetCore(obj, culture, format: null, out value);
}

private static bool ConvertToNullableDateTimeOffsetCore(object? obj, CultureInfo? culture, string? format, out DateTimeOffset? value)
private static bool ConvertToNullableDateTimeOffsetCore(object? obj, CultureInfo? culture, [StringSyntax(StringSyntaxAttribute.DateTimeFormat)] string? format, out DateTimeOffset? value)
{
var text = (string?)obj;
if (string.IsNullOrEmpty(text))
Expand Down
3 changes: 2 additions & 1 deletion src/Middleware/OutputCaching/test/TestUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Net.Http;
using System.Text;
using Microsoft.AspNetCore.Builder;
Expand All @@ -29,7 +30,7 @@ static TestUtils()
StreamUtilities.BodySegmentSize = 10;
}

private static bool TestRequestDelegate(HttpContext context, string guid)
private static bool TestRequestDelegate(HttpContext context, [StringSyntax(StringSyntaxAttribute.GuidFormat)] string guid)
{
var headers = context.Response.GetTypedHeaders();
headers.Date = DateTimeOffset.UtcNow;
Expand Down
3 changes: 2 additions & 1 deletion src/Middleware/ResponseCaching/test/TestUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net.Http;
using System.Text;
Expand Down Expand Up @@ -29,7 +30,7 @@ static TestUtils()
StreamUtilities.BodySegmentSize = 10;
}

private static bool TestRequestDelegate(HttpContext context, string guid)
private static bool TestRequestDelegate(HttpContext context, [StringSyntax(StringSyntaxAttribute.GuidFormat)] string guid)
{
var headers = context.Response.GetTypedHeaders();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO.Pipelines;
using System.Net;
using System.Security.Claims;
Expand Down Expand Up @@ -40,7 +41,7 @@ public DefaultConnectionContext() :
/// The caller is expected to set the <see cref="Transport"/> and <see cref="Application"/> pipes manually.
/// </summary>
/// <param name="id">The <see cref="ConnectionId"/>.</param>
public DefaultConnectionContext(string id)
public DefaultConnectionContext([StringSyntax(StringSyntaxAttribute.GuidFormat)] string id)
{
ConnectionId = id;

Expand All @@ -61,7 +62,7 @@ public DefaultConnectionContext(string id)
/// <param name="id">The <see cref="ConnectionId"/>.</param>
/// <param name="transport">The <see cref="Transport"/>.</param>
/// <param name="application">The <see cref="Application"/>.</param>
public DefaultConnectionContext(string id, IDuplexPipe transport, IDuplexPipe application)
public DefaultConnectionContext([StringSyntax(StringSyntaxAttribute.GuidFormat)] string id, IDuplexPipe transport, IDuplexPipe application)
: this(id)
{
Transport = transport;
Expand Down
10 changes: 5 additions & 5 deletions src/Servers/IIS/IIS/src/Core/IISHttpContext.Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ internal abstract partial class IISHttpContext
private static partial class Log
{
[LoggerMessage(1, LogLevel.Debug, @"Connection ID ""{ConnectionId}"" disconnecting.", EventName = "ConnectionDisconnect")]
public static partial void ConnectionDisconnect(ILogger logger, string connectionId);
public static partial void ConnectionDisconnect(ILogger logger, [StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId);

[LoggerMessage(2, LogLevel.Error, @"Connection ID ""{ConnectionId}"", Request ID ""{TraceIdentifier}"": An unhandled exception was thrown by the application.", EventName = "ApplicationError")]
public static partial void ApplicationError(ILogger logger, string connectionId, string traceIdentifier, Exception ex);
public static partial void ApplicationError(ILogger logger, [StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId, string traceIdentifier, Exception ex);

[LoggerMessage(3, LogLevel.Error, @"Unexpected exception in ""{ClassName}.{MethodName}"".", EventName = "UnexpectedError")]
public static partial void UnexpectedError(ILogger logger, string className, Exception ex, [CallerMemberName] string? methodName = null);

public static void ConnectionBadRequest(ILogger logger, string connectionId, Microsoft.AspNetCore.Http.BadHttpRequestException ex)
public static void ConnectionBadRequest(ILogger logger, [StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId, Microsoft.AspNetCore.Http.BadHttpRequestException ex)
=> ConnectionBadRequest(logger, connectionId, ex.Message, ex);

[LoggerMessage(4, LogLevel.Debug, @"Connection id ""{ConnectionId}"" bad request data: ""{message}""", EventName = nameof(ConnectionBadRequest))]
private static partial void ConnectionBadRequest(ILogger logger, string connectionId, string message, Microsoft.AspNetCore.Http.BadHttpRequestException ex);
private static partial void ConnectionBadRequest(ILogger logger, [StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId, string message, Microsoft.AspNetCore.Http.BadHttpRequestException ex);

[LoggerMessage(5, LogLevel.Debug, @"Connection ID ""{ConnectionId}"", Request ID ""{TraceIdentifier}"": The request was aborted by the client.", EventName = "RequestAborted")]
public static partial void RequestAborted(ILogger logger, string connectionId, string traceIdentifier);
public static partial void RequestAborted(ILogger logger, [StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId, string traceIdentifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal;

Expand All @@ -12,7 +13,7 @@ internal sealed class ConnectionLogScope : IReadOnlyList<KeyValuePair<string, ob

private string? _cachedToString;

public ConnectionLogScope(string connectionId)
public ConnectionLogScope([StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId)
{
_connectionId = connectionId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Buffers;
using System.Diagnostics;
using System.IO.Pipelines;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Internal;
using Microsoft.AspNetCore.Server.Kestrel.Core.Features;
Expand Down Expand Up @@ -69,7 +70,7 @@ internal class Http1OutputProducer : IHttpOutputProducer, IDisposable

public Http1OutputProducer(
PipeWriter pipeWriter,
string connectionId,
[StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId,
BaseConnectionContext connectionContext,
MemoryPool<byte> memoryPool,
KestrelTrace log,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Buffers;
using System.Buffers.Binary;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO.Pipelines;
using System.Net.Http.HPack;
using System.Threading.Channels;
Expand Down Expand Up @@ -58,7 +59,7 @@ public Http2FrameWriter(
int maxStreamsPerConnection,
ITimeoutControl timeoutControl,
MinDataRate? minResponseDataRate,
string connectionId,
[StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId,
MemoryPool<byte> memoryPool,
ServiceContext serviceContext)
{
Expand All @@ -82,7 +83,7 @@ public Http2FrameWriter(
// This is bounded by the maximum number of concurrent Http2Streams per Http2Connection.
// This isn't the same as SETTINGS_MAX_CONCURRENT_STREAMS, but typically double (with a floor of 100)
// which is the max number of Http2Streams that can end up in the Http2Connection._streams dictionary.
//
//
// Setting a lower limit of SETTINGS_MAX_CONCURRENT_STREAMS might be sufficient because a stream shouldn't
// be rescheduling itself after being completed or canceled, but we're going with the more conservative limit
// in case there's some logic scheduling completed or canceled streams unnecessarily.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http.Features;
Expand All @@ -13,7 +14,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http2;
internal sealed class Http2StreamContext : HttpConnectionContext
{
public Http2StreamContext(
string connectionId,
[StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId,
HttpProtocols protocols,
AltSvcHeader? altSvcHeader,
BaseConnectionContext connectionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Buffers;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO.Pipelines;
using System.Net.Http;
using System.Net.Http.QPack;
Expand Down Expand Up @@ -78,7 +79,7 @@ public Http3FrameWriter(ConnectionContext connectionContext, ITimeoutControl tim
: (int)clientPeerSettings.MaxRequestHeaderFieldSectionSize;
}

public void Reset(PipeWriter output, string connectionId)
public void Reset(PipeWriter output, [StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId)
{
_outputWriter = output;
_flusher.Initialize(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Buffers;
using System.Net;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
Expand All @@ -13,7 +14,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http3;
internal sealed class Http3StreamContext : HttpConnectionContext
{
public Http3StreamContext(
string connectionId,
[StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId,
HttpProtocols protocols,
AltSvcHeader? altSvcHeader,
BaseConnectionContext connectionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Buffers;
using System.IO.Pipelines;
using System.Net;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
Expand All @@ -13,7 +14,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
internal class HttpConnectionContext : BaseHttpConnectionContext
{
public HttpConnectionContext(
string connectionId,
[StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId,
HttpProtocols protocols,
AltSvcHeader? altSvcHeader,
BaseConnectionContext connectionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Buffers;
using System.Net;
using System.Diagnostics.CodeAnalysis;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
Expand All @@ -12,7 +13,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
internal sealed class HttpMultiplexedConnectionContext : BaseHttpConnectionContext
{
public HttpMultiplexedConnectionContext(
string connectionId,
[StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId,
HttpProtocols protocols,
AltSvcHeader? altSvcHeader,
MultiplexedConnectionContext connectionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void ConnectionStart(BaseConnectionContext connection)

[MethodImpl(MethodImplOptions.NoInlining)]
[Event(1, Level = EventLevel.Informational)]
private void ConnectionStart(string connectionId, string? localEndPoint, string? remoteEndPoint)
private void ConnectionStart([StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId, string? localEndPoint, string? remoteEndPoint)
{
WriteEvent(1, connectionId, localEndPoint, remoteEndPoint);
}
Expand All @@ -88,7 +88,7 @@ public void ConnectionStop(BaseConnectionContext connection)

[MethodImpl(MethodImplOptions.NoInlining)]
[Event(2, Level = EventLevel.Informational)]
private void ConnectionStop(string connectionId)
private void ConnectionStop([StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId)
{
WriteEvent(2, connectionId);
}
Expand All @@ -113,7 +113,7 @@ void Core(HttpProtocol httpProtocol)
}

[Event(3, Level = EventLevel.Informational)]
private void RequestStart(string connectionId, string requestId, string httpVersion, string path, string method)
private void RequestStart([StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId, string requestId, string httpVersion, string path, string method)
{
WriteEvent(3, connectionId, requestId, httpVersion, path, method);
}
Expand All @@ -138,14 +138,14 @@ void Core(HttpProtocol httpProtocol)
}

[Event(4, Level = EventLevel.Informational)]
private void RequestStop(string connectionId, string requestId, string httpVersion, string path, string method)
private void RequestStop([StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId, string requestId, string httpVersion, string path, string method)
{
WriteEvent(4, connectionId, requestId, httpVersion, path, method);
}

[MethodImpl(MethodImplOptions.NoInlining)]
[Event(5, Level = EventLevel.Informational)]
public void ConnectionRejected(string connectionId)
public void ConnectionRejected([StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId)
{
if (IsEnabled(EventLevel.Informational, EventKeywords.None))
{
Expand Down Expand Up @@ -179,7 +179,7 @@ public void TlsHandshakeStart(BaseConnectionContext connectionContext, SslServer

[MethodImpl(MethodImplOptions.NoInlining)]
[Event(8, Level = EventLevel.Informational)]
private void TlsHandshakeStart(string connectionId, string sslProtocols)
private void TlsHandshakeStart([StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId, string sslProtocols)
{
WriteEvent(8, connectionId, sslProtocols);
}
Expand All @@ -202,14 +202,14 @@ public void TlsHandshakeStop(BaseConnectionContext connectionContext, TlsConnect
[MethodImpl(MethodImplOptions.NoInlining)]
[Event(9, Level = EventLevel.Informational)]
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "Parameters passed to WriteEvent are all primative values.")]
private void TlsHandshakeStop(string connectionId, string sslProtocols, string applicationProtocol, string hostName)
private void TlsHandshakeStop([StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId, string sslProtocols, string applicationProtocol, string hostName)
{
WriteEvent(9, connectionId, sslProtocols, applicationProtocol, hostName);
}

[MethodImpl(MethodImplOptions.NoInlining)]
[Event(10, Level = EventLevel.Error)]
public void TlsHandshakeFailed(string connectionId)
public void TlsHandshakeFailed([StringSyntax(StringSyntaxAttribute.GuidFormat)] string connectionId)
{
Interlocked.Increment(ref _failedTlsHandshakes);

Expand Down
Loading