-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial validations generator for minimal APIs
- Loading branch information
1 parent
f7ff82f
commit ab949d5
Showing
81 changed files
with
4,847 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Builder.WebApplication.Conventions.get -> Microsoft.AspNetCore.Builder.IEndpointConventionBuilder! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/Http/Http.Abstractions/src/Metadata/IDisableValidationMetadata.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace Microsoft.AspNetCore.Http.Metadata; | ||
|
||
/// <summary> | ||
/// A marker interface which can be used to identify metadata that disables validation | ||
/// for a specific endpoint. | ||
/// </summary> | ||
public interface IDisableValidationMetadata | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
#nullable enable | ||
Microsoft.AspNetCore.Http.Metadata.IDisableValidationMetadata | ||
Microsoft.AspNetCore.Http.ProducesResponseTypeMetadata.Description.get -> string? | ||
Microsoft.AspNetCore.Http.ProducesResponseTypeMetadata.Description.set -> void | ||
Microsoft.AspNetCore.Http.Metadata.IProducesResponseTypeMetadata.Description.get -> string? |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
49 changes: 49 additions & 0 deletions
49
src/Http/Http.Extensions/gen/ValidationsGenerator/Emitters/ValidationsGenerator.Emitter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Collections.Immutable; | ||
using System.IO; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.AspNetCore.Http.ValidationsGenerator; | ||
|
||
public sealed partial class ValidationsGenerator | ||
{ | ||
internal static void EmitValidationsFile(SourceProductionContext context, ((string Left, string Right) Left, ImmutableArray<string> Right) source) | ||
{ | ||
var withValidations = source.Left.Left; | ||
var typeValidations = source.Left.Right; | ||
var validationsFilters = source.Right; | ||
var writer = new StringWriter(); | ||
var output = new CodeWriter(writer, baseIndent: 0); | ||
output.WriteLine("// <auto-generated/>"); | ||
output.WriteLine("#nullable enable"); | ||
output.WriteLine("namespace System.Runtime.CompilerServices"); | ||
output.StartBlock(); | ||
output.WriteLine("[AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)]"); | ||
output.WriteLine("file sealed class InterceptsLocationAttribute : Attribute"); | ||
output.StartBlock(); | ||
output.WriteLine("public InterceptsLocationAttribute(int version, string data) { }"); | ||
output.EndBlock(); | ||
output.EndBlock(); | ||
output.WriteLine(); | ||
output.WriteLine("namespace Microsoft.AspNetCore.Http.Validations.Generated"); | ||
output.StartBlock(); | ||
output.WriteLine("using System;"); | ||
output.WriteLine("using System.Linq;"); | ||
output.WriteLine("using System.Diagnostics;"); | ||
output.WriteLine("using System.ComponentModel.DataAnnotations;"); | ||
output.WriteLine(); | ||
output.Indent--; | ||
output.WriteLine(EmitEndpointKey()); | ||
output.WriteLine(EmitValidationProblemBuilder()); | ||
output.WriteLine(withValidations); | ||
output.WriteLine(); | ||
output.WriteLine(typeValidations); | ||
output.WriteLine(); | ||
output.Write(EmitEndpointValidationFilters(validationsFilters)); | ||
output.WriteLine("}"); | ||
output.WriteLine("#nullable restore"); | ||
context.AddSource("RouteHandlerValidations.g.cs", writer.ToString()); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...p.Extensions/gen/ValidationsGenerator/Emitters/ValidationsGenerator.EndpointKeyEmitter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.IO; | ||
|
||
namespace Microsoft.AspNetCore.Http.ValidationsGenerator; | ||
|
||
public sealed partial class ValidationsGenerator | ||
{ | ||
internal static string EmitEndpointKey() | ||
{ | ||
var writer = new StringWriter(); | ||
var code = new CodeWriter(writer, baseIndent: 1); | ||
code.WriteLine("file class EndpointKey(string route, global::System.Collections.Generic.IEnumerable<string> methods)"); | ||
code.StartBlock(); | ||
code.WriteLine("public string Route { get; } = route;"); | ||
code.WriteLine("public global::System.Collections.Generic.IEnumerable<string> Methods { get; } = methods;"); | ||
code.WriteLine(); | ||
code.WriteLine("public override bool Equals(object? obj)"); | ||
code.StartBlock(); | ||
code.WriteLine("if (obj is EndpointKey other)"); | ||
code.StartBlock(); | ||
code.WriteLine("return string.Equals(Route, other.Route, global::System.StringComparison.OrdinalIgnoreCase) &&"); | ||
code.WriteLine("Methods.SequenceEqual(other.Methods, global::System.StringComparer.OrdinalIgnoreCase);"); | ||
code.EndBlock(); | ||
code.WriteLine("return false;"); | ||
code.EndBlock(); | ||
code.WriteLine(); | ||
code.WriteLine("public override int GetHashCode()"); | ||
code.StartBlock(); | ||
code.WriteLine("int hash = 17;"); | ||
code.WriteLine("hash = hash * 23 + (Route?.GetHashCode(global::System.StringComparison.OrdinalIgnoreCase) ?? 0);"); | ||
code.WriteLine("hash = hash * 23 + GetMethodsHashCode(Methods);"); | ||
code.WriteLine("return hash;"); | ||
code.EndBlock(); | ||
code.WriteLine(); | ||
code.WriteLine("private static int GetMethodsHashCode(global::System.Collections.Generic.IEnumerable<string> methods)"); | ||
code.StartBlock(); | ||
code.WriteLine("if (methods == null)"); | ||
code.StartBlock(); | ||
code.WriteLine("return 0;"); | ||
code.EndBlock(); | ||
code.WriteLine("int hash = 17;"); | ||
code.WriteLine("foreach (var method in methods)"); | ||
code.StartBlock(); | ||
code.WriteLine("hash = hash * 23 + (method?.GetHashCode(global::System.StringComparison.OrdinalIgnoreCase) ?? 0);"); | ||
code.EndBlock(); | ||
code.WriteLine("return hash;"); | ||
code.EndBlock(); | ||
code.EndBlock(); | ||
return writer.ToString(); | ||
} | ||
} |
Oops, something went wrong.