Skip to content

[OpenAPI] Invalid schema references generated #63090

Closed

Description

@martincostello

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

To validate the changes made in #63052 I put the OpenAPI schema into editor.swagger.io to see if the schema for JSON Patch had any validation errors (it does not).

However, amongst a few schema errors that are found in the document that aren't just artifacts of testing, I found two that appear to be actual issues:

  1. Semantic error at components.schemas.Category.properties.tags.$ref - $refs must reference a valid location in the document from
  2. Semantic error at components.schemas.Item.properties.name.$ref - $refs must reference a valid location in the document from

These correlate to these parts of the code:

  1. public IEnumerable<Tag> Tags { get; set; } = [];
  2. public string[] Name { get; set; } = null!;

Both of these properties are arrays and appear to generate a circular reference to themselves via another object.

  1. Points to the tags property of the parent property of itself, but that's also a Category, so it's just a circular reference to itself.
  2. Points to the name property of the item1 property of Root, but that's also an Item, so again creates a circular reference to itself.

In both cases it looks like the schema properties should be an array of items which reference Tag or Item respectively.

Expected Behavior

The schemas do not generate validation errors.

Steps To Reproduce

Enter the contents of OpenApiDocumentLocalizationTests.VerifyOpenApiDocumentIsInvariant.verified.txt into editor.swagger.io.

Exceptions (if any)

No response

.NET Version

10.0.100-preview.7.25322.101

Anything else?

No response

Activity

  1. added
    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
    on Aug 4, 2025
  2. martincostello commented on Aug 4, 2025

    @martincostello
    MemberAuthor

    Have opened #63092 to add some validation and fix some issues, plus #63095 as a draft of a test that detects this issue.

  3. martincostello commented on Aug 4, 2025

    @martincostello
    MemberAuthor

    This might be a runtime issue, as it looks like the reference ID gets assigned when calling JsonSchemaExporter.GetJsonSchemaAsNode() here:

    private JsonNode CreateSchema(OpenApiSchemaKey key)
    => JsonSchemaExporter.GetJsonSchemaAsNode(_jsonSerializerOptions, key.Type, _configuration);

    That then gets used here:

    var relativeSchemaId = $"#/components/schemas/{rootSchemaId}{refIdString.Replace("#", string.Empty)}";
    return new OpenApiSchemaReference(relativeSchemaId, document);

  4. martincostello commented on Aug 4, 2025

    @martincostello
    MemberAuthor

    Actually I don't think it is, as the reference there correctly points to the inlined schema that's generated. Looks like it needs to get fixed-up somehow to point to be a valid pointer into the OpenAPI document.

  5. added this to the .NET 10 Planning milestone on Aug 4, 2025
  6. captainsafia commented on Aug 4, 2025

    @captainsafia
    Contributor

    @martincostello Yep -- I had roughly root caused this as the reason for bugs like #61194 (comment). I've been marinating on a fix though since "re-rooting" the relative references that STJ creates to the OpenAPI document is trickier for relative references that are two-layers deep.

  7. martincostello commented on Aug 15, 2025

    @martincostello
    MemberAuthor

    Looks like #63256 fixed this. #63092 should now pass and help detect any recurrence in the future.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-openapi

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions