Skip to content

Commit

Permalink
feat: filter by operation ids
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Jan 25, 2024
1 parent d326c01 commit 6f01e97
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 40 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -813,13 +813,13 @@ So, for example, if you would like to produce only the server code, you could
run `oapi-codegen -generate types,server`. You could generate `types` and
`server` into separate files, but both are required for the server code.

`oapi-codegen` can filter paths base on their tags in the openapi definition.
Use either `-include-tags` or `-exclude-tags` followed by a comma-separated list
of tags. For instance, to generate a server that serves all paths except those
tagged with `auth` or `admin`, use the argument, `-exclude-tags="auth,admin"`.
`oapi-codegen` can filter paths base on their tags or operationId in the openapi definition.
Use either `-include-tags`, `include-operation-ids` or `-exclude-tags`, `-exclude-operation-ids`
followed by a comma-separated list of tags or operation ids. For instance, to generate a server
that serves all paths except those tagged with `auth` or `admin`, use the argument, `-exclude-tags="auth,admin"`.
To generate a server that only handles `admin` paths, use the argument
`-include-tags="admin"`. When neither of these arguments is present, all paths
are generated.
`-include-tags="admin"` or use `-include-operation-ids="getPets"` to include this specific operation.
When neither of these arguments is present, all paths are generated.

`oapi-codegen` can filter schemas based on the option `--exclude-schemas`, which is
a comma separated list of schema names. For instance, `--exclude-schemas=Pet,NewPet`
Expand Down
47 changes: 30 additions & 17 deletions cmd/oapi-codegen/oapi-codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,15 @@ var (

// Deprecated: The options below will be removed in a future
// release. Please use the new config file format.
flagIncludeTags string
flagExcludeTags string
flagImportMapping string
flagExcludeSchemas string
flagResponseTypeSuffix string
flagAliasTypes bool
flagInitalismOverrides bool
flagIncludeTags string
flagExcludeTags string
flagIncludeOperationIDs string
flagExcludeOperationIDs string
flagImportMapping string
flagExcludeSchemas string
flagResponseTypeSuffix string
flagAliasTypes bool
flagInitalismOverrides bool
)

type configuration struct {
Expand All @@ -66,16 +68,18 @@ type configuration struct {
// oldConfiguration is deprecated. Please add no more flags here. It is here
// for backwards compatibility, and it will be removed in the future.
type oldConfiguration struct {
PackageName string `yaml:"package"`
GenerateTargets []string `yaml:"generate"`
OutputFile string `yaml:"output"`
IncludeTags []string `yaml:"include-tags"`
ExcludeTags []string `yaml:"exclude-tags"`
TemplatesDir string `yaml:"templates"`
ImportMapping map[string]string `yaml:"import-mapping"`
ExcludeSchemas []string `yaml:"exclude-schemas"`
ResponseTypeSuffix string `yaml:"response-type-suffix"`
Compatibility codegen.CompatibilityOptions `yaml:"compatibility"`
PackageName string `yaml:"package"`
GenerateTargets []string `yaml:"generate"`
OutputFile string `yaml:"output"`
IncludeTags []string `yaml:"include-tags"`
ExcludeTags []string `yaml:"exclude-tags"`
IncludeOperationIDs []string `yaml:"include-operation-ids"`
ExcludeOperationIDs []string `yaml:"exclude-operation-ids"`
TemplatesDir string `yaml:"templates"`
ImportMapping map[string]string `yaml:"import-mapping"`
ExcludeSchemas []string `yaml:"exclude-schemas"`
ResponseTypeSuffix string `yaml:"response-type-suffix"`
Compatibility codegen.CompatibilityOptions `yaml:"compatibility"`
}

// noVCSVersionOverride allows overriding the version of the application for cases where no Version Control System (VCS) is available when building, for instance when using a Nix derivation.
Expand All @@ -98,6 +102,8 @@ func main() {
`Comma-separated list of code to generate; valid options: "types", "client", "chi-server", "server", "gin", "gorilla", "spec", "skip-fmt", "skip-prune", "fiber", "iris".`)
flag.StringVar(&flagIncludeTags, "include-tags", "", "Only include operations with the given tags. Comma-separated list of tags.")
flag.StringVar(&flagExcludeTags, "exclude-tags", "", "Exclude operations that are tagged with the given tags. Comma-separated list of tags.")
flag.StringVar(&flagIncludeOperationIDs, "include-operation-ids", "", "Only include operations with the given operation-ids. Comma-separated list of operation-ids.")
flag.StringVar(&flagExcludeOperationIDs, "exclude-operation-ids", "", "Exclude operations with the given operation-ids. Comma-separated list of operation-ids.")
flag.StringVar(&flagTemplatesDir, "templates", "", "Path to directory containing user templates.")
flag.StringVar(&flagImportMapping, "import-mapping", "", "A dict from the external reference to golang package path.")
flag.StringVar(&flagExcludeSchemas, "exclude-schemas", "", "A comma separated list of schemas which must be excluded from generation.")
Expand Down Expand Up @@ -388,6 +394,13 @@ func updateConfigFromFlags(cfg *configuration) error {
if flagExcludeTags != "" {
cfg.OutputOptions.ExcludeTags = util.ParseCommandLineList(flagExcludeTags)
}
if flagIncludeOperationIDs != "" {
cfg.OutputOptions.IncludeOperationIDs = util.ParseCommandLineList(flagIncludeOperationIDs)
}
if flagExcludeOperationIDs != "" {
cfg.OutputOptions.ExcludeOperationIDs = util.ParseCommandLineList(flagExcludeOperationIDs)
}

if flagTemplatesDir != "" {
templates, err := loadTemplateOverrides(flagTemplatesDir)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/test/filter/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package client

//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --package=filtertags --generate=server -o tags/server.gen.go -include-tags included-tag1,included-tag2 server.yaml
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --package=filteroperations --generate=server -o operations/server.gen.go -include-operation-ids included-operation1,included-operation2 server.yaml
74 changes: 74 additions & 0 deletions internal/test/filter/operations/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions internal/test/filter/operations/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package filteroperations

import (
"testing"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

type server struct{}

func (s server) IncludedOperation1(ctx echo.Context) error {
return nil
}

func (s server) IncludedOperation2(ctx echo.Context) error {
return nil
}

func TestServer(t *testing.T) {
assert := assert.New(t)

var s ServerInterface = server{}
assert.NoError(s.IncludedOperation1(nil))
assert.NoError(s.IncludedOperation2(nil))
}
48 changes: 48 additions & 0 deletions internal/test/filter/server.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
openapi: "3.0.1"
info:
version: 1.0.0
title: Test Server
license:
name: MIT
description: |
This tests whether filtering works correctly
paths:
/included1:
get:
operationId: included-operation1
tags:
- included-tag1
responses:
200:
application/json:
schema:
$ref: '#/components/schemas/SchemaObject'
/filtered:
get:
operationId: filtered-operation
tags:
- filtered-tag
responses:
200:
application/json:
schema:
$ref: '#/components/schemas/SchemaObject'
/included2:
get:
operationId: included-operation2
tags:
- included-tag2
responses:
200:
application/json:
schema:
$ref: '#/components/schemas/SchemaObject'

components:
schemas:
SchemaObject:
properties:
firstName:
type: string
required:
- firstName
74 changes: 74 additions & 0 deletions internal/test/filter/tags/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions internal/test/filter/tags/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package filtertags

import (
"testing"

"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)

type server struct{}

func (s server) IncludedOperation1(ctx echo.Context) error {
return nil
}

func (s server) IncludedOperation2(ctx echo.Context) error {
return nil
}

func TestServer(t *testing.T) {
assert := assert.New(t)

var s ServerInterface = server{}
assert.NoError(s.IncludedOperation1(nil))
assert.NoError(s.IncludedOperation2(nil))
}
1 change: 1 addition & 0 deletions pkg/codegen/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func Generate(spec *openapi3.T, opts Configuration) (string, error) {
globalState.importMapping = constructImportMapping(opts.ImportMapping)

filterOperationsByTag(spec, opts)
filterOperationsByOperationID(spec, opts)
if !opts.OutputOptions.SkipPrune {
pruneUnusedComponents(spec)
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/codegen/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ type CompatibilityOptions struct {

// OutputOptions are used to modify the output code in some way.
type OutputOptions struct {
SkipFmt bool `yaml:"skip-fmt,omitempty"` // Whether to skip go imports on the generated code
SkipPrune bool `yaml:"skip-prune,omitempty"` // Whether to skip pruning unused components on the generated code
IncludeTags []string `yaml:"include-tags,omitempty"` // Only include operations that have one of these tags. Ignored when empty.
ExcludeTags []string `yaml:"exclude-tags,omitempty"` // Exclude operations that have one of these tags. Ignored when empty.
UserTemplates map[string]string `yaml:"user-templates,omitempty"` // Override built-in templates from user-provided files
SkipFmt bool `yaml:"skip-fmt,omitempty"` // Whether to skip go imports on the generated code
SkipPrune bool `yaml:"skip-prune,omitempty"` // Whether to skip pruning unused components on the generated code
IncludeTags []string `yaml:"include-tags,omitempty"` // Only include operations that have one of these tags. Ignored when empty.
ExcludeTags []string `yaml:"exclude-tags,omitempty"` // Exclude operations that have one of these tags. Ignored when empty.
IncludeOperationIDs []string `yaml:"include-operation-ids,omitempty"` // Only include operations that have one of these operation-ids. Ignored when empty.
ExcludeOperationIDs []string `yaml:"exclude-operation-ids,omitempty"` // Exclude operations that have one of these operation-ids. Ignored when empty.
UserTemplates map[string]string `yaml:"user-templates,omitempty"` // Override built-in templates from user-provided files

ExcludeSchemas []string `yaml:"exclude-schemas,omitempty"` // Exclude from generation schemas with given names. Ignored when empty.
ResponseTypeSuffix string `yaml:"response-type-suffix,omitempty"` // The suffix used for responses types
Expand Down
Loading

0 comments on commit 6f01e97

Please sign in to comment.