Skip to content
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

OpenApi won't Contains those Controllers If the Controller not starts with [ApiController] Attribute #58938

Closed
1 task done
yangbocheng opened this issue Nov 14, 2024 · 6 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved

Comments

@yangbocheng
Copy link

yangbocheng commented Nov 14, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Simply create a newest NET 9 aspnetcore webapi, then add Microsoft.AspNetCore.OpenApi and Scalar.AspNetCore. Then add a new Controller without [ApiController], this contoller's actions won't be included in openapi json

Expected Behavior

Controllers without [ApiController] should be included in openapi json

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

NET 9

Anything else?

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Nov 14, 2024
@yangbocheng
Copy link
Author

yangbocheng commented Nov 14, 2024

The reason to not use the [ApiController] is : Need manual validate some form fields, or api parameters in Project
OpenApi won't Contains those Controllers If the Controller not starts with [ApiController] Attribute

<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
  <PackageReference Include="Scalar.AspNetCore" Version="1.2.36" />
</ItemGroup>

so if remove the [ApiController] from Test1Controller.cs , then this won't show in openapi/v1.json
Image
Image
Image

@yangbocheng yangbocheng reopened this Nov 14, 2024
@martincostello martincostello added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi and removed area-web-frameworks *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Nov 14, 2024
@jaceks2106
Copy link

jaceks2106 commented Nov 15, 2024

Try to use Controller instead of ControllerBase as a base class.

Update:
If you want to apply custom validation on request parameters (query, form, header, whatever), then you should apply them as nullable. Then they won't be validated by OpenAPI. Then you can apply any custom validation you want in controller method.

@yangbocheng
Copy link
Author

I have tested again with the base : Controller and ControllerBase. They both don't work.
It seems that the only way to make openapi works well is to add [ApiController]
Image
Image

@captainsafia
Copy link
Member

@yangbocheng Thanks for filing this issue!

This is a known limitation of the ApiExplorer infrastructure, the intermediary layer in ASP.NET Core that provides information about registered implementors to OpenAPI tools.

In this particular case, only controllers that are attributed with [ApiController] are automatically documented by ASP.NET Core's infrastructure.

Building on top of @jaceks2106's guidance, why are you avoiding the attribution? There maybe other ways to customize the model binding/validation behavior to your desires.

@captainsafia captainsafia added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Dec 18, 2024
Copy link
Contributor

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@yangbocheng
Copy link
Author

Because [ApiController] will auto do things which i don't need.
So always i avoid to use it in project.

Follow Microsoft->means use the newest openapi-> means add [ApiController] to every controller-> so why not add this attribute to controller default? -> make bugs for multi programs who don't use apicontroller
Why it must add this limitation, it's very strange.

By the way, when I use the old Swagger, i don't need to add this attribute, everything work well.

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 feature-openapi ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. Status: Resolved
Projects
None yet
Development

No branches or pull requests

4 participants