-
Notifications
You must be signed in to change notification settings - Fork 10.2k
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
[DRAFT] Add initial validations generator for minimal APIs #59795
Draft
captainsafia
wants to merge
4
commits into
main
Choose a base branch
from
safia/validations-sg
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tdykstra @gewarren I removed one newline. Is there a convention for when to add a newline in
///
comments?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the product team has more particular rules around line length than we (content) do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, random newlines, depending on the width of you editor screen :)