Skip to content

OpenAPI: Filter endpoints based on GenerateControllerEndpoints usage #1450

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

Merged
merged 2 commits into from
Feb 12, 2024

Conversation

bkoelman
Copy link
Member

@bkoelman bkoelman commented Feb 3, 2024

This PR filters the listed JSON:API endpoints in the produced swagger.json.

Auto-generated controllers

When using auto-generated controllers, only the endpoints defined on the resource type are shown. For example:

[Resource(GenerateControllerEndpoints = JsonApiEndpoints.Post | JsonApiEndpoints.Patch)]
public sealed class Person : Identifiable<long>
{
    // ...
}

now only displays the corresponding endpoints:

image

This means that if you're adding a custom implementation of an action method in your partial class, you need to add its flag on [Resource] for it to become listed. If you don't, the method is assumed to exist solely to throw an exception when used.

This is needed because auto-generated controllers derive from JsonApiController<,>, which contains all JSON:API controller action methods. At runtime, an exception is thrown when the endpoint is not accessible.

Hand-written controllers

When not using auto-generated controllers, all exposed endpoints are listed. To hide some, derive from BaseJsonApiController instead of JsonApiController and include only the action methods that should be exposed by adding attributes like [HttpGet], [HttpPost], etc. There's no way to add action methods that always throw without them being listed.

Closes #1052.

QUALITY CHECKLIST

@bkoelman bkoelman force-pushed the openapi-filter-endpoints branch from 3787d5b to 376ede1 Compare February 3, 2024 01:14
Copy link

codecov bot commented Feb 3, 2024

Codecov Report

Attention: 6 lines in your changes are missing coverage. Please review.

Comparison is base (6c9aff8) 90.68% compared to head (71db1dd) 90.67%.

Files Patch % Lines
...ApiDotNetCore.OpenApi/OpenApiEndpointConvention.cs 83.87% 2 Missing and 3 partials ⚠️
...JsonApiMetadata/JsonApiEndpointMetadataProvider.cs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           openapi    #1450      +/-   ##
===========================================
- Coverage    90.68%   90.67%   -0.02%     
===========================================
  Files          390      390              
  Lines        12717    12735      +18     
  Branches      2056     2061       +5     
===========================================
+ Hits         11532    11547      +15     
- Misses         771      772       +1     
- Partials       414      416       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bkoelman bkoelman changed the title OpenAPI: Filter endpoints based on [Resource(GenerateControllerEndpoints = ...)] usage OpenAPI: Filter endpoints based on GenerateControllerEndpoints usage Feb 3, 2024
@bkoelman bkoelman force-pushed the openapi-filter-endpoints branch from 376ede1 to 71db1dd Compare February 10, 2024 23:41
@bkoelman bkoelman marked this pull request as ready for review February 11, 2024 00:29
@bkoelman bkoelman merged commit f7f8f8b into openapi Feb 12, 2024
@bkoelman bkoelman deleted the openapi-filter-endpoints branch February 12, 2024 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant