Skip to content

Conversation

@desjoerd
Copy link
Contributor

@desjoerd desjoerd commented Aug 7, 2025

OpenAPI: Apply descriptions from [Description] to the schema reference instead of the actual schema for properties

Update the json schema exporter for openapi to apply x-reference metadata for references.

Description

When a property of a class is a complex type and becomes a reference, the description was wrongfully applied to the actual schema instead of the reference.

Related previous PR #62213
Fixes #63175

Copilot AI review requested due to automatic review settings August 7, 2025 19:21
@desjoerd desjoerd requested review from a team and captainsafia as code owners August 7, 2025 19:21
@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Aug 7, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Aug 7, 2025

This comment was marked as outdated.

@desjoerd desjoerd requested a review from Copilot August 7, 2025 19:42
Copy link
Contributor

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 an issue where property descriptions from [Description] attributes were incorrectly applied to the actual schema instead of the schema reference when dealing with complex types in OpenAPI schema generation.

  • Updates the schema service to differentiate between inlined and referenced schemas when applying descriptions
  • Adds support for x-reference metadata annotations in the JSON schema
  • Introduces comprehensive test coverage for both referenced and inlined schema scenarios

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
OpenApiSchemaService.cs Modified schema generation logic to apply property descriptions to reference annotations for non-inlined schemas while preserving existing behavior for inlined schemas
OpenApiJsonSchema.Helpers.cs Added support for reading reference description and example annotations from JSON schema metadata
OpenApiSchemaService.Annotations.cs Added comprehensive test cases covering both referenced and inlined schema scenarios with various description attribute combinations

@gfoidl gfoidl added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Aug 8, 2025
if (attributeProvider.GetCustomAttributes(inherit: false).OfType<DefaultValueAttribute>().LastOrDefault() is DefaultValueAttribute defaultValueAttribute)
var propertyAttributes = attributeProvider.GetCustomAttributes(inherit: false);
var isInlinedSchema = schema[OpenApiConstants.SchemaId] is null;
if (isInlinedSchema)
Copy link
Member

Choose a reason for hiding this comment

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

I keep squinting at this and trying to figure out if it is a break that we now only apply validation attributes and default value attributes to property types that are inlined. I think we're fine since those attributes will typically only be applied to primitive values but this does feel a little spooky. Maybe we can reserve the isInlinedSchema just for the description stuff?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can do that, but I think it's more correct to not apply them for things which becoma a schema reference. It can only work when the schema of that property becomes a merged object using AllOf tricks. But even then, there are no Json Schema validations around object (except for a sub schema which is referenced in this case).

One semi-exception (before 3.1) was the required attribute, but that is already handled in the TypeInfoResolverModifier.

For validations and modifications on schemas which will be referenced it becomes a problem when two properties for the same schema give conflicting information, because they would both apply the attributes, which makes the last property overwrite the validations of the first property.

Let me whether to keep it like this or make it limited to the description 😊. I can also add a comment explaining this in the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated it to be limited to the description only, because I am going on holiday for 2 weeks. Then I will have time to add more tests for the validation attributes, and default value. So we can be confident to exclude them for schema references.

@desjoerd
Copy link
Contributor Author

desjoerd commented Aug 8, 2025

@captainsafia thank you for your review.

I've removed the x-ref-example handling in the JsonSchema exporter as there is no [Example] attributes.

I've replied to the second comment let me know if you agree, or that I limit it to the description.

@desjoerd
Copy link
Contributor Author

desjoerd commented Aug 9, 2025

On the comment for the SchemaService, I've now limited the change to only the description. So you can merge it without fear 😊. I will be on holiday for 2 weeks, so I won't be able to work on it in the coming 2 weeks (I still got my phone so I will watch it). When I am back (if not someone else has done it) will add some more tests around Schema references and validation attributes. And the support of the default annotation.

@captainsafia
Copy link
Member

On the comment for the SchemaService, I've now limited the change to only the description. So you can merge it without fear 😊. I will be on holiday for 2 weeks, so I won't be able to work on it in the coming 2 weeks (I still got my phone so I will watch it). When I am back (if not someone else has done it) will add some more tests around Schema references and validation attributes. And the support of the default annotation.

Thanks! I generally agree that not applying validation/default valeus to references is probably the correct thing to do but the change felt subtle enough that we should sleep on it. Enjoy your holiday and I look forward to more PRs when you get back! 😄

@captainsafia captainsafia enabled auto-merge (squash) August 10, 2025 02:04
@captainsafia captainsafia merged commit c7fc087 into dotnet:main Aug 10, 2025
29 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Aug 10, 2025
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: Description Attribute is not applied to schema references

3 participants