Skip to content

Conversation

desjoerd
Copy link
Contributor

@desjoerd desjoerd commented Sep 2, 2025

OpenAPI: Fix Circular reference in specific order gives empty schema

In the ResolveReferenceForSchema method it could be that when executed in a certain order with a circular reference that it would register the schema without properties.

Description

In the ResolveReferenceForSchema method it now no longer registers schemas with a $ref as a full schema. They will eventually be registered as the method is recursive.

Fixes #63503

@Copilot Copilot AI review requested due to automatic review settings September 2, 2025 16:40
@desjoerd desjoerd requested review from captainsafia and a team as code owners September 2, 2025 16:40
Copilot

This comment was marked as outdated.

@github-actions github-actions bot added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Sep 2, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Sep 2, 2025
@desjoerd
Copy link
Contributor Author

desjoerd commented Sep 2, 2025

This was a head-scratcher 😅. The fun of recursive methods 😛.

@desjoerd
Copy link
Contributor Author

desjoerd commented Sep 3, 2025

@captainsafia I know it was yesterday, but I remember why it's in two seperate groups in the sample. If one of the property orders is correct the referencedmodel schema is emitted. So it's in the rare case when the circular reference is in the specific order, and the referenced model is not used in any other endpoint.

I will remove it from the sample (it's not really adding value there) and just create two seperate isolated unit tests in the schema service tests.

@captainsafia
Copy link
Member

@desjoerd Sounds like a good plan to just use unit tests to validate this -- thanks!

@desjoerd desjoerd force-pushed the feature/openapi-circular branch from 1f27ab9 to f159bed Compare September 4, 2025 09:36
@desjoerd desjoerd requested a review from Copilot September 4, 2025 10:49
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug in the OpenAPI schema generation service where circular references processed in a specific order could result in empty schemas. The fix changes how schema references are handled to prevent premature registration of incomplete schemas.

Key changes:

  • Modified the ResolveReferenceForSchema method to return a reference instead of registering the schema immediately
  • Added comprehensive test coverage for circular reference scenarios with different property orders

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
OpenApiSchemaService.cs Changed schema reference resolution to prevent premature registration of incomplete schemas
OpenApiSchemaService.SchemaReferences.cs Added test cases covering circular reference scenarios with different property ordering

@desjoerd
Copy link
Contributor Author

desjoerd commented Sep 4, 2025

@captainsafia I've moved the tests 😊. Luckily the same methods where available so an easy change. Let me know if you prever them higher up or not.

I am not fond of #region but that unit tests file screams it a little bit 😅. Maybe something for the future when the fun starts around enabling Schema Reference transformations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates community-contribution Indicates that the PR has been added by a community member feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAPI: Circular reference in specific order gives empty schema
3 participants