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

Fix interceptor JSON casing #2907

Merged
merged 1 commit into from
May 21, 2024
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
10 changes: 6 additions & 4 deletions src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIOptions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json.Serialization;
using System.Reflection;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Swashbuckle.AspNetCore.SwaggerUI
{
Expand Down Expand Up @@ -233,7 +233,7 @@ public class OAuthConfigObject
/// <summary>
/// String array of initially selected oauth scopes, default is empty array
/// </summary>
public IEnumerable<string> Scopes { get; set; } = new string[] { };
public IEnumerable<string> Scopes { get; set; } = [];

/// <summary>
/// Additional query parameters added to authorizationUrl and tokenUrl
Expand Down Expand Up @@ -262,6 +262,7 @@ public class InterceptorFunctions
/// Accepts one argument requestInterceptor(request) and must return the modified request, or a Promise that resolves to the modified request.
/// Ex: "function (req) { req.headers['MyCustomHeader'] = 'CustomValue'; return req; }"
/// </summary>
[JsonPropertyName("RequestInterceptorFunction")]
Copy link
Owner

@domaindrivendev domaindrivendev May 21, 2024

Choose a reason for hiding this comment

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

Given that the rest of the config properties are serialized to camel case, I wonder would it be better to maintain that consistency and just update the assignment in index.html?

var interceptors = JSON.parse('%(Interceptors)');
if (interceptors.RequestInterceptorFunction)
configObject.requestInterceptor = parseFunction(interceptors.RequestInterceptorFunction);
if (interceptors.ResponseInterceptorFunction)
configObject.responseInterceptor = parseFunction(interceptors.ResponseInterceptorFunction);

Copy link
Owner

Choose a reason for hiding this comment

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

Just a thought but not a showstopper for me other than that looks good 👍

Copy link
Owner

Choose a reason for hiding this comment

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

doh didn't realize we had auto-merge - oh well I guess we leave it as is

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I did consider renaming it, but this persuaded me to leave it alone: #2906 (comment)

public string RequestInterceptorFunction { get; set; }

/// <summary>
Expand All @@ -270,6 +271,7 @@ public class InterceptorFunctions
/// Accepts one argument responseInterceptor(response) and must return the modified response, or a Promise that resolves to the modified response.
/// Ex: "function (res) { console.log(res); return res; }"
/// </summary>
[JsonPropertyName("ResponseInterceptorFunction")]
public string ResponseInterceptorFunction { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ public async Task IndexUrl_ReturnsCustomIndexHtml_IfConfigured()
Assert.Contains("Example.com", content);
}

[Fact]
public async Task IndexUrl_ReturnsInterceptors_IfConfigured()
{
var client = new TestSite(typeof(CustomUIConfig.Startup)).BuildClient();

var response = await client.GetAsync("/swagger/index.html");
var content = await response.Content.ReadAsStringAsync();

Assert.Contains("\"RequestInterceptorFunction\":", content);
Assert.Contains("\"ResponseInterceptorFunction\":", content);
}

[Theory]
[InlineData("/swagger/index.html", new [] { "Version 1.0", "Version 2.0" })]
[InlineData("/swagger/1.0/index.html", new [] { "Version 1.0" })]
Expand Down
Loading