Skip to content

Commit

Permalink
Fix serialization of AdditionalItems
Browse files Browse the repository at this point in the history
Extend custom `JsonSerializerContext` to cover objects that might be added to the additional JSON property.
Resolves #2884,
  • Loading branch information
martincostello committed May 18, 2024
1 parent 91c936b commit ac588fe
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
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
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

0 comments on commit ac588fe

Please sign in to comment.