Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/GeneratedEndpoints/AddEndpointHandlersGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void GenerateSource(SourceProductionContext context, ImmutableSort
context.CancellationToken.ThrowIfCancellationRequested();

var nonStaticClassNames = grouped.Keys
.Where(x => !x.IsStatic)
.Where(x => !x.IsStatic && !x.IsAbstract)
.Select(x => x.Name)
.ToList();
var source = new StringBuilder();
Expand Down
100 changes: 100 additions & 0 deletions src/GeneratedEndpoints/Common/Constants.AcceptsAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using System.Text;
using Microsoft.CodeAnalysis.Text;

namespace GeneratedEndpoints.Common;

internal static partial class Constants
{
internal const string AcceptsAttributeName = "AcceptsAttribute";
internal const string AcceptsAttributeHint = $"{AcceptsAttributeFullyQualifiedName}.gs.cs";

private const string AcceptsAttributeFullyQualifiedName = $"{AttributesNamespace}.{AcceptsAttributeName}";

internal static readonly SourceText AcceptsAttributeSourceText = CreateAcceptsAttributeSourceText();

private static SourceText CreateAcceptsAttributeSourceText() => SourceText.From($$"""
{{FileHeader}}

namespace {{AttributesNamespace}};

/// <summary>
/// Specifies the request type and content types accepted by the annotated endpoint or class.
/// </summary>
[global::System.AttributeUsage(global::System.AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
internal sealed class {{AcceptsAttributeName}} : global::System.Attribute
{
/// <summary>
/// Gets the CLR type of the endpoint filter.
/// </summary>
public global::System.Type Type { get; }

/// <summary>
/// Gets the primary content type accepted by the endpoint.
/// </summary>
public string ContentType { get; }

/// <summary>
/// Gets the additional content types accepted by the endpoint.
/// </summary>
public string[] AdditionalContentTypes { get; }

/// <summary>
/// Gets a value indicating whether the request body is optional.
/// </summary>
public bool IsOptional { get; init; }

/// <summary>
/// Initializes a new instance of the <see cref="{{AcceptsAttributeName}}"/> class.
/// </summary>
/// <param name="type">The CLR type of the request body.</param>
/// <param name="contentType">The primary content type accepted by the endpoint.</param>
/// <param name="additionalContentTypes">Additional content types accepted by the endpoint.</param>
public {{AcceptsAttributeName}}(global::System.Type type, string contentType = "application/json", params string[] additionalContentTypes)
{
Type = type;
ContentType = contentType;
AdditionalContentTypes = additionalContentTypes;
}
}

/// <summary>
/// Specifies the request type using a generic argument and the content types accepted by the annotated endpoint or class.
/// </summary>
/// <typeparam name="TRequest">The CLR type of the request body.</typeparam>
[global::System.AttributeUsage(global::System.AttributeTargets.Method, Inherited = false, AllowMultiple = true)]
internal sealed class {{AcceptsAttributeName}}<TRequest> : global::System.Attribute
{
/// <summary>
/// Gets the CLR type of the endpoint filter.
/// </summary>
public global::System.Type Type => typeof(TRequest);

/// <summary>
/// Gets the primary content type accepted by the endpoint.
/// </summary>
public string ContentType { get; }

/// <summary>
/// Gets the additional content types accepted by the endpoint.
/// </summary>
public string[] AdditionalContentTypes { get; }

/// <summary>
/// Gets a value indicating whether the request body is optional.
/// </summary>
public bool IsOptional { get; init; }

/// <summary>
/// Initializes a new instance of the generic Accepts attribute class.
/// </summary>
/// <param name="contentType">The primary content type accepted by the endpoint.</param>
/// <param name="additionalContentTypes">Additional content types accepted by the endpoint.</param>
public {{AcceptsAttributeName}}(string contentType = "application/json", params string[] additionalContentTypes)
{
ContentType = contentType;
AdditionalContentTypes = additionalContentTypes;
}
}

""", Encoding.UTF8);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text;
using Microsoft.CodeAnalysis.Text;

namespace GeneratedEndpoints.Common;

internal static partial class Constants
{
internal const string DisableAntiforgeryAttributeName = "DisableAntiforgeryAttribute";
internal const string DisableAntiforgeryAttributeHint = $"{DisableAntiforgeryAttributeFullyQualifiedName}.gs.cs";

private const string DisableAntiforgeryAttributeFullyQualifiedName = $"{AttributesNamespace}.{DisableAntiforgeryAttributeName}";

internal static readonly SourceText DisableAntiforgeryAttributeSourceText = CreateDisableAntiforgeryAttributeSourceText();

private static SourceText CreateDisableAntiforgeryAttributeSourceText() => SourceText.From($$"""
{{FileHeader}}

namespace {{AttributesNamespace}};

/// <summary>
/// Disables antiforgery protection for the annotated endpoint or class.
/// </summary>
[global::System.AttributeUsage(global::System.AttributeTargets.Class | global::System.AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
internal sealed class {{DisableAntiforgeryAttributeName}} : global::System.Attribute
{
}

""", Encoding.UTF8);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text;
using Microsoft.CodeAnalysis.Text;

namespace GeneratedEndpoints.Common;

internal static partial class Constants
{
internal const string DisableRequestTimeoutAttributeName = "DisableRequestTimeoutAttribute";
internal const string DisableRequestTimeoutAttributeHint = $"{DisableRequestTimeoutAttributeFullyQualifiedName}.gs.cs";

private const string DisableRequestTimeoutAttributeFullyQualifiedName = $"{AttributesNamespace}.{DisableRequestTimeoutAttributeName}";

internal static readonly SourceText DisableRequestTimeoutAttributeSourceText = CreateDisableRequestTimeoutAttributeSourceText();

private static SourceText CreateDisableRequestTimeoutAttributeSourceText() => SourceText.From($$"""
{{FileHeader}}

namespace {{AttributesNamespace}};

/// <summary>
/// Disables the request timeout for the annotated endpoint or class.
/// </summary>
[global::System.AttributeUsage(global::System.AttributeTargets.Class | global::System.AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
internal sealed class {{DisableRequestTimeoutAttributeName}} : global::System.Attribute
{
}

""", Encoding.UTF8);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Text;
using Microsoft.CodeAnalysis.Text;

namespace GeneratedEndpoints.Common;

internal static partial class Constants
{
internal const string DisableValidationAttributeName = "DisableValidationAttribute";
internal const string DisableValidationAttributeHint = $"{DisableValidationAttributeFullyQualifiedName}.gs.cs";

private const string DisableValidationAttributeFullyQualifiedName = $"{AttributesNamespace}.{DisableValidationAttributeName}";

internal static readonly SourceText DisableValidationAttributeSourceText = CreateDisableValidationAttributeSourceText();

private static SourceText CreateDisableValidationAttributeSourceText() => SourceText.From($$"""
#if NET10_0_OR_GREATER
{{FileHeader}}

namespace {{AttributesNamespace}};

/// <summary>
/// Disables request validation for the annotated endpoint or class when targeting .NET 10 or later.
/// </summary>
[global::System.AttributeUsage(global::System.AttributeTargets.Class | global::System.AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
internal sealed class {{DisableValidationAttributeName}} : global::System.Attribute
{
}
#endif

""", Encoding.UTF8);
}
Loading