Skip to content
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

Joperezr/merge rc2 changes #4555

Merged
merged 3 commits into from
Oct 12, 2023
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
167 changes: 153 additions & 14 deletions src/Generators/Microsoft.Gen.AutoClient/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,13 @@ potentialNamespaceParent is not NamespaceDeclarationSyntax &&
return result;
}

private static ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<AttributeData> methodAttributes, SymbolHolder symbols)
private ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<AttributeData> methodAttributes, SymbolHolder symbols)
{
List<string?> httpMethods = [];
string? requestName = null;
string? path = null;
bool requestNameFailed = false;
bool headersParsingFailed = false;
Dictionary<string, string> staticHeaders = [];

foreach (var methodAttribute in methodAttributes)
Expand All @@ -342,7 +344,19 @@ private static ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<
continue;
}

path = methodAttribute.ConstructorArguments[0].Value as string;
var methodArg = methodAttribute.ConstructorArguments[0];
if (methodArg.IsNull)
{
continue;
}

var argString = methodArg.Value as string;
if (string.IsNullOrEmpty(argString))
{
continue;
}

path = argString;
}
else if (attributeSymbol.Equals(symbols.RestPostAttribute, SymbolEqualityComparer.Default))
{
Expand All @@ -353,7 +367,19 @@ private static ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<
continue;
}

path = methodAttribute.ConstructorArguments[0].Value as string;
var methodArg = methodAttribute.ConstructorArguments[0];
if (methodArg.IsNull)
{
continue;
}

var argString = methodArg.Value as string;
if (string.IsNullOrEmpty(argString))
{
continue;
}

path = argString;
}
else if (attributeSymbol.Equals(symbols.RestPutAttribute, SymbolEqualityComparer.Default))
{
Expand All @@ -364,7 +390,19 @@ private static ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<
continue;
}

path = methodAttribute.ConstructorArguments[0].Value as string;
var methodArg = methodAttribute.ConstructorArguments[0];
if (methodArg.IsNull)
{
continue;
}

var argString = methodArg.Value as string;
if (string.IsNullOrEmpty(argString))
{
continue;
}

path = argString;
}
else if (attributeSymbol.Equals(symbols.RestDeleteAttribute, SymbolEqualityComparer.Default))
{
Expand All @@ -375,7 +413,19 @@ private static ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<
continue;
}

path = methodAttribute.ConstructorArguments[0].Value as string;
var methodArg = methodAttribute.ConstructorArguments[0];
if (methodArg.IsNull)
{
continue;
}

var argString = methodArg.Value as string;
if (string.IsNullOrEmpty(argString))
{
continue;
}

path = argString;
}
else if (attributeSymbol.Equals(symbols.RestPatchAttribute, SymbolEqualityComparer.Default))
{
Expand All @@ -386,7 +436,19 @@ private static ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<
continue;
}

path = methodAttribute.ConstructorArguments[0].Value as string;
var methodArg = methodAttribute.ConstructorArguments[0];
if (methodArg.IsNull)
{
continue;
}

var argString = methodArg.Value as string;
if (string.IsNullOrEmpty(argString))
{
continue;
}

path = argString;
}
else if (attributeSymbol.Equals(symbols.RestOptionsAttribute, SymbolEqualityComparer.Default))
{
Expand All @@ -397,7 +459,19 @@ private static ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<
continue;
}

path = methodAttribute.ConstructorArguments[0].Value as string;
var methodArg = methodAttribute.ConstructorArguments[0];
if (methodArg.IsNull)
{
continue;
}

var argString = methodArg.Value as string;
if (string.IsNullOrEmpty(argString))
{
continue;
}

path = argString;
}
else if (attributeSymbol.Equals(symbols.RestHeadAttribute, SymbolEqualityComparer.Default))
{
Expand All @@ -408,7 +482,19 @@ private static ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<
continue;
}

path = methodAttribute.ConstructorArguments[0].Value as string;
var methodArg = methodAttribute.ConstructorArguments[0];
if (methodArg.IsNull)
{
continue;
}

var argString = methodArg.Value as string;
if (string.IsNullOrEmpty(argString))
{
continue;
}

path = argString;
}
else if (attributeSymbol.Equals(symbols.RestStaticHeaderAttribute, SymbolEqualityComparer.Default))
{
Expand All @@ -417,28 +503,67 @@ private static ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<
continue;
}

var key = methodAttribute.ConstructorArguments[0].Value as string;
var value = methodAttribute.ConstructorArguments[1].Value as string;
var keyArg = methodAttribute.ConstructorArguments[0];
var valueArg = methodAttribute.ConstructorArguments[1];

if (key == null || value == null)
if (keyArg.IsNull)
{
Diag(DiagDescriptors.ErrorInvalidHeaderName, attributeSymbol.GetLocation());
headersParsingFailed = true;
continue;
}

staticHeaders.Add(key, value);
if (valueArg.IsNull)
{
Diag(DiagDescriptors.ErrorInvalidHeaderValue, attributeSymbol.GetLocation());
headersParsingFailed = true;
continue;
}

var keyString = keyArg.Value as string;
var valueString = valueArg.Value as string;

if (string.IsNullOrEmpty(keyString))
{
Diag(DiagDescriptors.ErrorInvalidHeaderName, attributeSymbol.GetLocation());
headersParsingFailed = true;
continue;
}

if (valueString == null)
{
Diag(DiagDescriptors.ErrorInvalidHeaderValue, attributeSymbol.GetLocation());
headersParsingFailed = true;
continue;
}

staticHeaders.Add(keyString!, valueString);
}

foreach (var a in methodAttribute.NamedArguments)
{
if (a.Key == RequestNamePropertyName)
{
requestName = (string)a.Value.Value!;
if (a.Value.IsNull)
{
requestNameFailed = true;
continue;
}

var valueString = a.Value.Value as string;
if (string.IsNullOrEmpty(valueString))
{
requestNameFailed = true;
continue;
}

requestName = valueString;
break;
}
}
}

return new(httpMethods, path, requestName, staticHeaders);
return new(httpMethods, path, requestName, requestNameFailed, headersParsingFailed, staticHeaders);
}

private RestApiMethod? ProcessMethod(
Expand Down Expand Up @@ -474,6 +599,18 @@ private static ParseMethodAttributesResult ParseMethodAttributes(ImmutableArray<
hasErrors = true;
}

if (methodAttrResult.RequestNameParsingFailed)
{
Diag(DiagDescriptors.ErrorInvalidRequestName, methodSymbol.GetLocation());
hasErrors = true;
}

if (methodAttrResult.HeadersParsingFailed)
{
// Diagnostics are already emitted from the parsing method
hasErrors = true;
}

var returnTypeSymbol = (INamedTypeSymbol)methodSymbol.ReturnType;
ITypeSymbol? innerType = null;

Expand Down Expand Up @@ -662,5 +799,7 @@ private sealed record class ParseMethodAttributesResult(
List<string?> HttpMethods,
string? Path,
string? RequestName,
bool RequestNameParsingFailed,
bool HeadersParsingFailed,
Dictionary<string, string> StaticHeaders);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class DeleteAttribute : Attribute
/// <summary>
/// Initializes a new instance of the <see cref="DeleteAttribute"/> class.
/// </summary>
/// <param name="path">The path of the request.</param>
/// <param name="path">The path of the request. Cannot be empty or null.</param>
public DeleteAttribute(string path)
{
Path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class GetAttribute : Attribute
/// <summary>
/// Initializes a new instance of the <see cref="GetAttribute"/> class.
/// </summary>
/// <param name="path">The path of the request.</param>
/// <param name="path">The path of the request. Cannot be empty or null.</param>
public GetAttribute(string path)
{
Path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class HeadAttribute : Attribute
/// <summary>
/// Initializes a new instance of the <see cref="HeadAttribute"/> class.
/// </summary>
/// <param name="path">The path of the request.</param>
/// <param name="path">The path of the request. Cannot be empty or null.</param>
public HeadAttribute(string path)
{
Path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class OptionsAttribute : Attribute
/// <summary>
/// Initializes a new instance of the <see cref="OptionsAttribute"/> class.
/// </summary>
/// <param name="path">The path of the request.</param>
/// <param name="path">The path of the request. Cannot be empty or null.</param>
public OptionsAttribute(string path)
{
Path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class PatchAttribute : Attribute
/// <summary>
/// Initializes a new instance of the <see cref="PatchAttribute"/> class.
/// </summary>
/// <param name="path">The path of the request.</param>
/// <param name="path">The path of the request. Cannot be empty or null.</param>
public PatchAttribute(string path)
{
Path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class PostAttribute : Attribute
/// <summary>
/// Initializes a new instance of the <see cref="PostAttribute"/> class.
/// </summary>
/// <param name="path">The path of the request.</param>
/// <param name="path">The path of the request. Cannot be empty or null.</param>
public PostAttribute(string path)
{
Path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public sealed class PutAttribute : Attribute
/// <summary>
/// Initializes a new instance of the <see cref="PutAttribute"/> class.
/// </summary>
/// <param name="path">The path of the request.</param>
/// <param name="path">The path of the request. Cannot be empty or null.</param>
public PutAttribute(string path)
{
Path = path;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Microsoft.Extensions.Http.AutoClient;
/// Injects a static header to be sent with every request. When this attribute is applied
/// to an interface, then it impacts every method described by the interface. Otherwise, it only
/// affects the method where it is applied.
/// The header name must not be null or empty. The value, on the other hand, can be empty, but not null.
/// </remarks>
/// <example>
/// <code>
Expand All @@ -33,8 +34,8 @@ public sealed class StaticHeaderAttribute : Attribute
/// <summary>
/// Initializes a new instance of the <see cref="StaticHeaderAttribute"/> class.
/// </summary>
/// <param name="header">The name of the header.</param>
/// <param name="value">The value of the header.</param>
/// <param name="header">The name of the header. Cannot be empty or null.</param>
/// <param name="value">The value of the header. Cannot be null.</param>
public StaticHeaderAttribute(string header, string value)
{
Header = header;
Expand Down
Loading