Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 20 additions & 0 deletions src/Refitter.Core/ParameterExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,26 @@ public static IEnumerable<string> GetParameters(
.ToList();
}

if (settings.GenerateAuthenticationHeader)
{
var document = operation.Parent.Parent;
foreach (var securitySchemeName in operationModel.Security.SelectMany(x => x.Keys))
{
if (!document.SecurityDefinitions.TryGetValue(securitySchemeName, out var securityScheme))
{
continue;
}

if (securityScheme.Type == OpenApiSecuritySchemeType.ApiKey
&& securityScheme.In == OpenApiSecurityApiKeyLocation.Header
&& !operationModel.Parameters.Any(p => p.Kind == OpenApiParameterKind.Header && p.IsHeader && p.Name == securityScheme.Name))
{
headerParameters.Add($"[Header(\"{securityScheme.Name}\")] string {securityScheme.Name}");
break;

Copilot AI Apr 23, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of 'break;' here stops processing after the first matching security scheme. If there may be multiple applicable authentication header schemes, ensure this behavior is intended or consider removing the break to process all valid schemes.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed break as all security schemas should be processed.

}
}
}

var formParameters = operationModel.Parameters
.Where(p => p.Kind == OpenApiParameterKind.FormData && !p.IsBinaryBodyParameter)
.Select(p =>
Expand Down
6 changes: 6 additions & 0 deletions src/Refitter.Core/Settings/RefitGeneratorSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,10 @@ payloads with (yet) unknown types are offered by newer versions of an API
public bool UsePolymorphicSerialization { get; set; }

[JsonIgnore] public IParameterNameGenerator? ParameterNameGenerator { get; set; }

/// <summary>
/// Gets or sets a value indicating whether to generate Security Schema Authentication headers.
/// </summary>
[Description("Generate Security Schema Authentication headers")]
public bool GenerateAuthenticationHeader { get; set; }
}
54 changes: 53 additions & 1 deletion src/Refitter.Tests/Resources/EmbeddedResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,50 @@ public static string SwaggerPetstoreYamlV3WithDifferentHeaders
}
}

public static string SwaggerPetstoreJsonV2WithAuthenticationHeaders
{
get
{
using var stream = GetStream("V2.SwaggerPetstoreWithAuthenticationHeaders.json");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}

public static string SwaggerPetstoreJsonV3WithAuthenticationHeaders
{
get
{
using var stream = GetStream("V3.SwaggerPetstoreWithAuthenticationHeaders.json");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}

public static string SwaggerPetstoreYamlV2WithAuthenticationHeaders
{
get
{
using var stream = GetStream("V2.SwaggerPetstoreWithAuthenticationHeaders.yaml");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}

public static string SwaggerPetstoreYamlV3WithAuthenticationHeaders
{
get
{
using var stream = GetStream("V3.SwaggerPetstoreWithAuthenticationHeaders.yaml");
using var reader = new StreamReader(stream);

return reader.ReadToEnd();
}
}

public static string SwaggerIllegalPathsJsonV3
{
get
Expand Down Expand Up @@ -134,7 +178,15 @@ public static string GetSwaggerPetstore(SampleOpenSpecifications version)
SwaggerPetstoreYamlV2WithDifferentHeaders,
SampleOpenSpecifications.SwaggerPetstoreYamlV3WithDifferentHeaders =>
SwaggerPetstoreYamlV3WithDifferentHeaders,
SampleOpenSpecifications.SwaggerPetstoreJsonV2WithAuthenticationHeaders =>
SwaggerPetstoreJsonV2WithAuthenticationHeaders,
SampleOpenSpecifications.SwaggerPetstoreJsonV3WithAuthenticationHeaders =>
SwaggerPetstoreJsonV3WithAuthenticationHeaders,
SampleOpenSpecifications.SwaggerPetstoreYamlV2WithAuthenticationHeaders =>
SwaggerPetstoreYamlV2WithAuthenticationHeaders,
SampleOpenSpecifications.SwaggerPetstoreYamlV3WithAuthenticationHeaders =>
SwaggerPetstoreYamlV3WithAuthenticationHeaders,
_ => throw new ArgumentOutOfRangeException(nameof(version), version, null)
};
}
}
}
8 changes: 6 additions & 2 deletions src/Refitter.Tests/Resources/SampleOpenSpecifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public enum SampleOpenSpecifications
SwaggerPetstoreJsonV2WithDifferentHeaders,
SwaggerPetstoreJsonV3WithDifferentHeaders,
SwaggerPetstoreYamlV2WithDifferentHeaders,
SwaggerPetstoreYamlV3WithDifferentHeaders,
SwaggerPetstoreYamlV3WithDifferentHeaders,
SwaggerPetstoreJsonV2WithAuthenticationHeaders,
SwaggerPetstoreJsonV3WithAuthenticationHeaders,
SwaggerPetstoreYamlV2WithAuthenticationHeaders,
SwaggerPetstoreYamlV3WithAuthenticationHeaders,
IllegalPathsJsonV3
}
}
Loading