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

[Bug]: disabling Swagger "syntaxHighlight" fails with "Metadata for type 'System.Boolean' was not provided to the serializer" in 6.6 #2884

Closed
WolfgangHG opened this issue May 15, 2024 · 3 comments · Fixed by #2890
Assignees
Labels
Milestone

Comments

@WolfgangHG
Copy link

Describe the bug

I use this code snippet to disable syntax highlighting for json responses, as I create really large responses and the syntax highlighting does not complete/hangs up with 6.5:

public void Configure(IApplicationBuilder app)
{
  ...
  app.UseSwaggerUI(config =>
  {
    ...
    config.ConfigObject.AdditionalItems.Add("syntaxHighlight", false);
  });
  ...
}

Source for this: https://stackoverflow.com/questions/64053444/setting-syntaxhighlight-property-for-swagger-ui-with-swashbuckle-aspnetcore (and https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/)

After updating from 6.5.0 to 6.6.1, the url "https://localhost:1235/swagger/index.html" shows this error:

System.NotSupportedException: Metadata for type 'System.Boolean' was not provided to the serializer. The serializer method used does not support reflection-based creation of serialization-related type metadata. If using source generation, ensure that all root types passed to the serializer have been indicated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically. The unsupported member type is located on type 'System.Object'. Path: $.AdditionalItems.
 ---> System.NotSupportedException: Metadata for type 'System.Boolean' was not provided to the serializer. The serializer method used does not support reflection-based creation of serialization-related type metadata. If using source generation, ensure that all root types passed to the serializer have been indicated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_NoMetadataForType(Type type)
   at System.Text.Json.JsonSerializerOptions.GetClassFromContextOrCreate(Type type)
   at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type)
   at System.Text.Json.WriteStackFrame.InitializeReEntry(Type type, JsonSerializerOptions options)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Converters.DictionaryOfTKeyTValueConverter`3.OnWriteResume(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWriteDataExtensionProperty(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJsonExtensionData(Object obj, WriteStack& state, Utf8JsonWriter writer)
   at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.Converters.JsonMetadataServicesConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex)
   at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
   at System.Text.Json.JsonSerializer.WriteUsingSerializer[TValue](Utf8JsonWriter writer, TValue& value, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.WriteUsingGeneratedSerializer[TValue](Utf8JsonWriter writer, TValue& value, JsonTypeInfo jsonTypeInfo)
   at System.Text.Json.JsonSerializer.WriteStringUsingGeneratedSerializer[TValue](TValue& value, JsonTypeInfo jsonTypeInfo)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.GetIndexArguments()
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.RespondWithIndexHtml(HttpResponse response)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

As the error mentions the path "AdditionalItems", I was pointed to the config switch. Removing the line makes the service work again.

I include the packages "Swashbuckle.AspNetCore", "Swashbuckle.AspNetCore.Annotations" and "Swashbuckle.AspNetCore.Newtonsoft", the service also uses "Microsoft.AspNetCore.Mvc.NewtonsoftJson". But the error message says that "System.Text.Json" was used here.

Expected behavior

No response

Actual behavior

No response

Steps to reproduce

No response

Exception(s) (if any)

No response

Swashbuckle.AspNetCore version

No response

.NET Version

No response

Anything else?

No response

@WolfgangHG WolfgangHG added the bug label May 15, 2024
@martincostello
Copy link
Collaborator

Thanks for the report - this is a regression caused by #2800.

@martincostello martincostello self-assigned this May 15, 2024
martincostello added a commit to martincostello/Swashbuckle.AspNetCore that referenced this issue May 15, 2024
Extend custom `JsonSerializerContext` to cover objects that might be added to the additional JSON property.
Resolves domaindrivendev#2884,
@martincostello martincostello added this to the v6.6.2 milestone May 15, 2024
martincostello added a commit that referenced this issue May 18, 2024
Extend custom `JsonSerializerContext` to cover objects that might be added to the additional JSON property.
Resolves #2884,
@martincostello
Copy link
Collaborator

Thanks for reporting this issue - the fix is available in Swashbuckle.AspNetCore 6.6.2.

@WolfgangHG
Copy link
Author

I can confirm that "AdditionalItems" work again. Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants