Skip to content

.NET 9 OpenApi: JSON schema generation is not supported for contracts using ReferenceHandler.Preserve #58943

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

Open
1 task done
nicolaiarocci opened this issue Nov 14, 2024 · 5 comments
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi

Comments

@nicolaiarocci
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Upgrading a .NET 8 Minimal API to .NET 9 and switching from Swashbuckle.AspNetCore to built-in Open API support, we get the following exception:

System.NotSupportedException: JSON schema generation is not supported for contracts using ReferenceHandler.Preserve.

Indeed, we have references preserved in our JSON options:

public static void AddJsonOptions(this WebApplicationBuilder builder)
    {
        builder.Services.Configure<JsonOptions>(options =>
        {
            options.SerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
            options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower;
            options.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
        });
    }

By commenting out the ReferenceHandler.Preserve, the open api document is created as expected.

Expected Behavior

Support for contracts using ReferenceHandler.Preserve.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

9.0.100

Anything else?

No response

@ghost ghost added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Nov 14, 2024
@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Nov 14, 2024
@captainsafia
Copy link
Member

@nicolaiarocci Thanks for filing this issue!

@eiriktsarpalis Is there a particular reason we opted not to generate schemas when ReferenceHandler.Preserve = true?

It seems like the schemas that Swashbuckle creates ignore the ReferenceHandler setting altogether:

"Person": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "nullable": true
          },
          "child": {
            "$ref": "#/components/schemas/Person"
          },
          "parent": {
            "$ref": "#/components/schemas/Person"
          }
        },
        "additionalProperties": false
      },

for:

class Person
{
    public string? Name { get; set; }
    public Person? Child { get; set; }
    public Person? Parent { get; set; }
}

@eiriktsarpalis
Copy link
Member

Reference preservation alters the schema for most types, notably with collections that end up wrapping their values in JSON objects. Support for it was cut in .NET 9 for lack of time, but it's conceivable that it could be added in the future provided there is demand.

@eiriktsarpalis
Copy link
Member

I should add that enabling reference preservation in your app carries important security ramifications. It should only be used with trusted inputs produced by System.Text.Json. In my opinion, it is not appropriate to use reference preservation in the context of web apps or Open API.

@alfonsovgs
Copy link

I should add that enabling reference preservation in your app carries important security ramifications. It should only be used with trusted inputs produced by System.Text.Json. In my opinion, it is not appropriate to use reference preservation in the context of web apps or Open API.

Helo @eiriktsarpalis

I haven't understood well, I have that same error but also this one:
The depth of the generated JSON schema exceeds the JsonSerializerOptions.MaxDepth setting.

Could you help me understand why this happens and how to avoid it?

@eiriktsarpalis
Copy link
Member

This looks like a different error. Could you file an issue in dotnet/runtime with a standalone reproduction? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-openapi
Projects
None yet
Development

No branches or pull requests

5 participants