Skip to content

[OpenAPI] Integers represented as both integer/string even when AddJsonOptions sets NumberHandling to Strict #64145

@IGx89

Description

@IGx89

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In order to configure a controller API app to both accept only numbers for integer properties and represent them as only integers (and not integer/strings) in the OpenAPI document, JSON options have to be configured in two places:

builder.Services
	.AddControllers()
	.AddJsonOptions(options =>
	{
		// Tell controllers to not accept strings for number fields
		options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.Strict;
	});

builder.Services.ConfigureHttpJsonOptions(options =>
{
	// Tell OpenApi generator to report number fields as integers/floats only, not strings
	options.SerializerOptions.NumberHandling = JsonNumberHandling.Strict;
});

Typically only one or the other are configured (former for controller-based APIs and the latter for minimal APIs). Is that intentional or a bug that OpenAPI ignores Microsoft.AspNetCore.Mvc.JsonOptions?

Related to #61038

Expected Behavior

NumberHandling only needs to be set to strict on Microsoft.AspNetCore.Mvc.JsonOptions to cause OpenAPI to report number fields as not strings.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

10.0.100-rc.2.25502.107

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions