Add OpenAPI tags support using existing tag system #1980
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add OpenAPI Tags Support to Encore Client Generation
Overview
This PR implements support for OpenAPI tags in Encore's API directives, allowing developers to organize their endpoints in the generated OpenAPI specification with custom tags using Encore's existing tag infrastructure.
Motivation
Currently, Encore generates OpenAPI specifications without custom organization tags, making it difficult for developers to group related endpoints in documentation and client generation tools. OpenAPI tags are a standard way to organize endpoints into logical groups (e.g., "Users", "Management", "Admin").
This feature addresses the need for better endpoint organization in generated OpenAPI specs while maintaining backward compatibility with existing API directives and leveraging Encore's proven tag system.
Changes
Core Implementation
1. Enhanced API Endpoint Structure
File:
v2/parser/apis/api/api.go
OpenAPITags []string
field toEndpoint
structtag:openapi-*
directivesopenapi-
prefix from tag names2. Error Handling
File:
v2/parser/apis/api/errors.go
errInvalidOpenAPITag
error for invalid OpenAPI tag validation3. Protocol Buffer Schema
File:
proto/encore/parser/meta/v1/meta.proto
repeated string openapi_tags = 20;
to RPC message.pb.go
files with proper field support4. OpenAPI Generation
File:
pkg/clientgen/openapi/openapi.go
rpc.GetOpenapiTags()
in spec generation5. Metadata Conversion
File:
v2/app/legacymeta/legacymeta.go
ep.OpenAPITags
torpc.OpenapiTags
during conversionTesting
API Parser Tests
File:
v2/parser/apis/api/api_test.go
tag:openapi-Users
tag:openapi-*
directivestag:openapi-Users tag:middleware-auth
)tag:openapi-
)OpenAPI Generation Tests
File:
pkg/clientgen/openapi/openapi_test.go
Implementation Approach
This implementation leverages Encore's existing tag system rather than creating new directive fields. This approach provides several benefits:
openapi-
prefix clearly separates from middleware tagsUsage
Developers can now use OpenAPI tags in their API directives using the existing tag system with an
openapi-
namespace:Generated OpenAPI Impact
The above examples generate OpenAPI specification with proper tags:
Backward Compatibility
This implementation is fully backward compatible:
Testing
All tests pass:
Related
Note: This implementation uses Encore's existing tag infrastructure with an
openapi-
namespace prefix, ensuring clean separation from middleware functionality while maintaining consistency with established patterns.