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 serialization of AdditionalItems #2890

Merged
merged 3 commits into from
May 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<Target Name="NpmInstall" BeforeTargets="DispatchToInnerBuilds">
<Target Name="NpmInstall" BeforeTargets="DispatchToInnerBuilds" Condition=" '$(CI)' != '' OR !Exists('$(MSBuildThisFileDirectory)\node_modules') ">
<Exec Command="npm install" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
Expand Down
2 changes: 1 addition & 1 deletion src/Swashbuckle.AspNetCore.Swagger/SwaggerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SwaggerMiddleware(
{
_next = next;
_options = options ?? new SwaggerOptions();
_requestMatcher = new TemplateMatcher(TemplateParser.Parse(_options.RouteTemplate), new RouteValueDictionary());
_requestMatcher = new TemplateMatcher(TemplateParser.Parse(_options.RouteTemplate), []);
}

#if !NETSTANDARD
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
#if NET6_0_OR_GREATER
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

namespace Swashbuckle.AspNetCore.SwaggerUI;

[JsonSerializable(typeof(ConfigObject))]
[JsonSerializable(typeof(InterceptorFunctions))]
[JsonSerializable(typeof(OAuthConfigObject))]
// These primitive types are declared for common types that may be used with ConfigObject.AdditionalItems. See https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2884.
[JsonSerializable(typeof(bool))]
[JsonSerializable(typeof(byte))]
[JsonSerializable(typeof(sbyte))]
[JsonSerializable(typeof(short))]
[JsonSerializable(typeof(ushort))]
[JsonSerializable(typeof(int))]
[JsonSerializable(typeof(uint))]
[JsonSerializable(typeof(long))]
[JsonSerializable(typeof(ulong))]
[JsonSerializable(typeof(float))]
[JsonSerializable(typeof(double))]
[JsonSerializable(typeof(decimal))]
[JsonSerializable(typeof(char))]
[JsonSerializable(typeof(string))]
[JsonSerializable(typeof(DateTime))]
[JsonSerializable(typeof(DateTimeOffset))]
[JsonSerializable(typeof(TimeSpan))]
[JsonSerializable(typeof(JsonArray))]
[JsonSerializable(typeof(JsonObject))]
[JsonSerializable(typeof(JsonDocument))]
#if NET7_0_OR_GREATER
[JsonSerializable(typeof(DateOnly))]
[JsonSerializable(typeof(TimeOnly))]
#endif
#if NET8_0_OR_GREATER
[JsonSerializable(typeof(Half))]
[JsonSerializable(typeof(Int128))]
[JsonSerializable(typeof(UInt128))]
#endif
[JsonSourceGenerationOptions(
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
Expand Down
4 changes: 2 additions & 2 deletions src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public async Task Invoke(HttpContext httpContext)
await _staticFileMiddleware.Invoke(httpContext);
}

private StaticFileMiddleware CreateStaticFileMiddleware(
private static StaticFileMiddleware CreateStaticFileMiddleware(
RequestDelegate next,
IWebHostEnvironment hostingEnv,
ILoggerFactory loggerFactory,
Expand All @@ -104,7 +104,7 @@ private StaticFileMiddleware CreateStaticFileMiddleware(
return new StaticFileMiddleware(next, hostingEnv, Options.Create(staticFileOptions), loggerFactory);
}

private void RespondWithRedirect(HttpResponse response, string location)
private static void RespondWithRedirect(HttpResponse response, string location)
{
response.StatusCode = 301;
response.Headers["Location"] = location;
Expand Down
4 changes: 2 additions & 2 deletions src/Swashbuckle.AspNetCore.SwaggerUI/SwaggerUIOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public class ConfigObject
public string ValidatorUrl { get; set; } = null;

[JsonExtensionData]
public Dictionary<string, object> AdditionalItems { get; set; } = new Dictionary<string, object>();
public Dictionary<string, object> AdditionalItems { get; set; } = [];
}

public class UrlDescriptor
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>

<Target Name="NpmInstall" BeforeTargets="DispatchToInnerBuilds">
<Target Name="NpmInstall" BeforeTargets="DispatchToInnerBuilds" Condition=" '$(CI)' != '' OR !Exists('$(MSBuildThisFileDirectory)\node_modules') ">
<Exec Command="npm install" ContinueOnError="true">
<Output TaskParameter="ExitCode" PropertyName="ErrorCode" />
</Exec>
Expand Down
32 changes: 31 additions & 1 deletion test/WebSites/CustomUIConfig/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.OpenApi.Models;
using Swashbuckle.AspNetCore.SwaggerUI;

namespace CustomUIConfig
Expand Down Expand Up @@ -76,6 +78,34 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
c.UseResponseInterceptor("(res) => { console.log('Custom interceptor intercepted response from:', res.url); return res; }");
c.EnablePersistAuthorization();

c.ConfigObject.AdditionalItems.Add("syntaxHighlight", false);
c.ConfigObject.AdditionalItems.Add("charProperty", 'c');
c.ConfigObject.AdditionalItems.Add("stringProperty", "value");
c.ConfigObject.AdditionalItems.Add("byteProperty", (byte)1);
c.ConfigObject.AdditionalItems.Add("signedByteProperty", (sbyte)1);
c.ConfigObject.AdditionalItems.Add("int16Property", (short)1);
c.ConfigObject.AdditionalItems.Add("uint16Property", (ushort)1);
c.ConfigObject.AdditionalItems.Add("int32Property", 1);
c.ConfigObject.AdditionalItems.Add("uint32Property", 1u);
c.ConfigObject.AdditionalItems.Add("int64Property", 1L);
c.ConfigObject.AdditionalItems.Add("uint64Property", 1uL);
c.ConfigObject.AdditionalItems.Add("floatProperty", 1f);
c.ConfigObject.AdditionalItems.Add("doubleProperty", 1d);
c.ConfigObject.AdditionalItems.Add("decimalProperty", 1m);
c.ConfigObject.AdditionalItems.Add("dateTimeProperty", DateTime.UtcNow);
c.ConfigObject.AdditionalItems.Add("dateTimeOffsetProperty", DateTimeOffset.UtcNow);
c.ConfigObject.AdditionalItems.Add("timeSpanProperty", new TimeSpan(12, 34, 56));
c.ConfigObject.AdditionalItems.Add("jsonArray", new JsonArray() { "string" });
c.ConfigObject.AdditionalItems.Add("jsonObject", new JsonObject() { ["foo"] = "bar" });
c.ConfigObject.AdditionalItems.Add("jsonDocument", JsonDocument.Parse("""{ "foo": "bar" }"""));

#if NET8_0_OR_GREATER
c.ConfigObject.AdditionalItems.Add("dateOnlyProperty", new DateOnly(1977, 05, 25));
c.ConfigObject.AdditionalItems.Add("timeOnlyProperty", new TimeOnly(12, 34, 56));
c.ConfigObject.AdditionalItems.Add("halfProperty", Half.CreateChecked(1));
c.ConfigObject.AdditionalItems.Add("int128Property", Int128.CreateChecked(1));
c.ConfigObject.AdditionalItems.Add("unt128Property", UInt128.CreateChecked(1));
#endif
});
}
}
Expand Down
Loading