-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Swagger transformation * Swagger transformation example * Update readme with swagger transformation * Improved swagger transformation efficiency * fix of sample apps * Prevent exception for missing factories and return early * Refactored ApplySwaggerTransformation
- Loading branch information
Showing
7 changed files
with
248 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
using Microsoft.OpenApi.Models; | ||
using System.Net; | ||
using Yarp.ReverseProxy.Swagger; | ||
using Yarp.ReverseProxy.Transforms; | ||
using Yarp.ReverseProxy.Transforms.Builder; | ||
|
||
namespace Yarp.Transformations; | ||
|
||
public class HeaderTransformFactory : ITransformFactory, ISwaggerTransformFactory | ||
{ | ||
/// <summary> | ||
/// Property validates for header titel rename | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="transformValues"></param> | ||
/// <returns></returns> | ||
public bool Validate(TransformRouteValidationContext context, IReadOnlyDictionary<string, string> transformValues) | ||
{ | ||
if (transformValues.TryGetValue("RenameHeader", out var header)) | ||
{ | ||
if (string.IsNullOrEmpty(header)) | ||
{ | ||
context.Errors.Add(new ArgumentException("A non-empty RenameHeader value is required")); | ||
} | ||
|
||
if (transformValues.TryGetValue("Set", out var newHeader)) | ||
{ | ||
if (string.IsNullOrEmpty(newHeader)) | ||
{ | ||
context.Errors.Add(new ArgumentException("A non-empty Set value is required")); | ||
} | ||
} | ||
else | ||
{ | ||
context.Errors.Add(new ArgumentException("Set option is required")); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Header title rename transformation | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="transformValues"></param> | ||
/// <returns></returns> | ||
/// <exception cref="ArgumentException"></exception> | ||
public bool Build(TransformBuilderContext context, IReadOnlyDictionary<string, string> transformValues) | ||
{ | ||
if (transformValues.TryGetValue("RenameHeader", out var header)) | ||
{ | ||
if (string.IsNullOrEmpty(header)) | ||
{ | ||
throw new ArgumentException("A non-empty RenameHeader value is required"); | ||
} | ||
|
||
if (transformValues.TryGetValue("Set", out var newHeader)) | ||
{ | ||
if (string.IsNullOrEmpty(newHeader)) | ||
{ | ||
throw new ArgumentException("A non-empty Set value is required"); | ||
} | ||
} | ||
else | ||
{ | ||
throw new ArgumentException("Set option is required"); | ||
} | ||
|
||
context.AddRequestTransform(transformContext => | ||
{ | ||
if (transformContext.ProxyRequest.Headers.TryGetValues(header, out var headerValue)) | ||
{ | ||
// Remove the original header | ||
transformContext.ProxyRequest.Headers.Remove(header); | ||
|
||
// Add a new header with the same value(s) as the original header | ||
transformContext.ProxyRequest.Headers.Add(newHeader, headerValue); | ||
} | ||
|
||
return default; | ||
}); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Header title rename transformation for Swagger | ||
/// </summary> | ||
/// <param name="operation"></param> | ||
/// <param name="transformValues"></param> | ||
/// <returns></returns> | ||
public bool Build(OpenApiOperation operation, IReadOnlyDictionary<string, string> transformValues) | ||
{ | ||
if (transformValues.ContainsKey("RenameHeader")) | ||
{ | ||
foreach (var parameter in operation.Parameters) | ||
{ | ||
if (parameter.In.HasValue && parameter.In.Value.ToString().Equals("Header")) | ||
{ | ||
if (transformValues.TryGetValue("RenameHeader", out var header) | ||
&& transformValues.TryGetValue("Set", out var newHeader)) | ||
{ | ||
if (parameter.Name == newHeader) | ||
{ | ||
parameter.Name = header; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.OpenApi.Models; | ||
using System.Collections.Generic; | ||
|
||
namespace Yarp.ReverseProxy.Swagger | ||
{ | ||
public interface ISwaggerTransformFactory | ||
{ | ||
bool Build(OpenApiOperation operation, IReadOnlyDictionary<string, string> transformValues); | ||
} | ||
} |
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