-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Closed
Labels
Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.The author of this issue needs to respond in order for us to continue investigating this issue.area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi
Milestone
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
In response to: #56318 (comment)
JsonUnmappedMemberHandling.Disallow
does not seem to work.
If I enable it for this object
class ExampleObject
{
public int ExampleProperty { get; init; }
}
I get
"ExampleObject": {
"type": "object",
"properties": {
"exampleProperty": {
"type": "integer",
"format": "int32"
}
}
}
or an exception, depending on the way of configuration.
Expected Behavior
I expect to see
"ExampleObject": {
"type": "object",
"additionalProperties": false,
"properties": {
"exampleProperty": {
"type": "integer",
"format": "int32"
}
}
}
Steps To Reproduce
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
// This seems to do nothing
builder.Services.ConfigureHttpJsonOptions(options =>
options.SerializerOptions.UnmappedMemberHandling = JsonUnmappedMemberHandling.Disallow);
var app = builder.Build();
app.MapGet("/hello", () => new ExampleObject
{
ExampleProperty = 123
});
app.MapOpenApi();
app.Run();
// This attributes causes an error
[JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Disallow)]
class ExampleObject
{
public int ExampleProperty { get; init; }
}
Exceptions (if any)
System.Text.Json.JsonException: 'Expected StartObject token to represent beginning of schema.'
at OpenApiJsonSchema.JsonConverter.Read(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options) in -\OpenApiJsonSchema.cs:line 21
presumably because additional properties is expected to be an object, and not False
.
.NET Version
9.0.100-rc.1.24452.12
Anything else?
Something like the transformer below provides the expected behavior for my test cases
internal class DisableAdditionalPropertiesTransformer : IOpenApiSchemaTransformer
{
public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken)
{
if (schema is { Type: "object", AdditionalProperties: null } &&
context.JsonTypeInfo.Kind is not JsonTypeInfoKind.Dictionary &&
!context.JsonTypeInfo.Properties.Any(property => property.IsExtensionData))
{
schema.AdditionalPropertiesAllowed = false;
}
return Task.CompletedTask;
}
}
Metadata
Metadata
Assignees
Labels
Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.The author of this issue needs to respond in order for us to continue investigating this issue.area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi