Skip to content

Commit

Permalink
chore(): upstream v2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Yousuf Jawwad committed Apr 30, 2024
2 parents ecea152 + ad5eada commit 43c9ded
Show file tree
Hide file tree
Showing 138 changed files with 5,500 additions and 576 deletions.
1 change: 1 addition & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_extends: oapi-codegen/.github
25 changes: 25 additions & 0 deletions .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Release Drafter

on:
push:
branches:
- master
workflow_dispatch: {}

permissions:
contents: read

jobs:
update_release_draft:
permissions:
contents: write
pull-requests: write
runs-on: ubuntu-latest
steps:
- uses: release-drafter/release-drafter@v5
with:
name: next
tag: next
version: next
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ help:
@echo " tidy tidy go mod"

$(GOBIN)/golangci-lint:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.54.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOBIN) v1.55.2

.PHONY: tools
tools: $(GOBIN)/golangci-lint
Expand Down
51 changes: 40 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func SetupHandler() {
var myApi PetStoreImpl

r := gin.Default()
r.Use(middleware.OapiRequestValidator(swagger))
r.Use(middleware.OapiRequestValidator(swagger))
r = api.RegisterHandlers(r, petStore)
}
```
Expand Down Expand Up @@ -343,7 +343,7 @@ func SetupHandler() {
var myApi PetStoreImpl

i := iris.Default()
i.Use(middleware.OapiRequestValidator(swagger))
i.Use(middleware.OapiRequestValidator(swagger))
api.RegisterHandlers(r, &myApi)
}
```
Expand Down Expand Up @@ -383,7 +383,7 @@ The generated strict wrapper can then be used as an implementation for `ServerIn
```go
func SetupHandler() {
var myApi PetStoreImpl
myStrictApiHandler := api.NewStrictHandler(myApi, nil)
myStrictApiHandler := api.NewStrictHandler(myApi, nil)
e := echo.New()
petstore.RegisterHandlers(e, &myStrictApiHandler)
}
Expand Down Expand Up @@ -619,7 +619,7 @@ which help you to use the various OpenAPI 3 Authentication mechanism.
}
// Example providing your own provider using an anonymous function wrapping in the
// InterceptoFn adapter. The behaviour between the InterceptorFn and the Interceptor interface
// InterceptorFn adapter. The behaviour between the InterceptorFn and the Interceptor interface
// are the same as http.HandlerFunc and http.Handler.
customProvider := func(req *http.Request, ctx context.Context) error {
// Just log the request header, nothing else.
Expand Down Expand Up @@ -779,6 +779,35 @@ which help you to use the various OpenAPI 3 Authentication mechanism.
type ObjectCategory int
```

- `x-order`: specifies the order of the fields in the structure. It allows you to specify the order
of the fields in the generated structure and will override any default value. This extended
property is not supported in all parts of OpenAPI, so check the specification to see where it is
allowed.

```yaml
DateInterval:
type: object
required:
- name
properties:
end:
type: string
format: date
x-order: 2
start:
type: string
format: date
x-order: 1
```
In the example above, struct will be declared as:
```go
type DateInterval struct {
Start *openapi_types.Date `json:"start,omitempty"`
End *openapi_types.Date `json:"end,omitempty"`
}
```

## Using `oapi-codegen`

The default options for `oapi-codegen` will generate everything; client, server,
Expand Down Expand Up @@ -813,13 +842,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 Expand Up @@ -933,7 +962,7 @@ output-options:
# using a local file
client-with-responses.tmpl: /home/username/workspace/templatesProject/my-client-with-responses.tmpl
# The following are referencing a versuion of the default
# The following are referencing a version of the default
# client-with-responses.tmpl file, but loaded in through
# github's raw.githubusercontent.com. The general form
# to use raw.githubusercontent.com is as follows
Expand Down
69 changes: 43 additions & 26 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
flagInitialismOverrides 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,12 +102,14 @@ 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.")
flag.StringVar(&flagResponseTypeSuffix, "response-type-suffix", "", "The suffix used for responses types.")
flag.BoolVar(&flagAliasTypes, "alias-types", false, "Alias type declarations of possible.")
flag.BoolVar(&flagInitalismOverrides, "initialism-overrides", false, "Use initialism overrides.")
flag.BoolVar(&flagInitialismOverrides, "initialism-overrides", false, "Use initialism overrides.")

flag.Parse()

Expand Down Expand Up @@ -240,7 +246,13 @@ func main() {
errExit("error parsing'%s' as YAML: %v\n", flagConfigFile, err)
}
}
opts = newConfigFromOldConfig(oldConfig)
var err error
opts, err = newConfigFromOldConfig(oldConfig)
if err != nil {
flag.PrintDefaults()
errExit("error creating new config from old config: %v\n", err)
}

}

// Ensure default values are set if user hasn't specified some needed
Expand Down Expand Up @@ -382,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 Expand Up @@ -410,7 +429,7 @@ func updateConfigFromFlags(cfg *configuration) error {
cfg.OutputFile = flagOutputFile
}

cfg.OutputOptions.InitialismOverrides = flagInitalismOverrides
cfg.OutputOptions.InitialismOverrides = flagInitialismOverrides

return nil
}
Expand Down Expand Up @@ -488,7 +507,7 @@ func generationTargets(cfg *codegen.Configuration, targets []string) error {
return nil
}

func newConfigFromOldConfig(c oldConfiguration) configuration {
func newConfigFromOldConfig(c oldConfiguration) (configuration, error) {
// Take flags into account.
cfg := updateOldConfigFromFlags(c)

Expand All @@ -500,9 +519,7 @@ func newConfigFromOldConfig(c oldConfiguration) configuration {
opts.OutputOptions.ResponseTypeSuffix = flagResponseTypeSuffix

if err := generationTargets(&opts, cfg.GenerateTargets); err != nil {
fmt.Println(err)
flag.PrintDefaults()
os.Exit(1)
return configuration{}, fmt.Errorf("generation targets: %w", err)
}

opts.OutputOptions.IncludeTags = cfg.IncludeTags
Expand All @@ -511,7 +528,7 @@ func newConfigFromOldConfig(c oldConfiguration) configuration {

templates, err := loadTemplateOverrides(cfg.TemplatesDir)
if err != nil {
errExit("error loading template overrides: %s\n", err)
return configuration{}, fmt.Errorf("loading template overrides: %w", err)
}
opts.OutputOptions.UserTemplates = templates

Expand All @@ -522,5 +539,5 @@ func newConfigFromOldConfig(c oldConfiguration) configuration {
return configuration{
Configuration: opts,
OutputFile: cfg.OutputFile,
}
}, nil
}
16 changes: 8 additions & 8 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ replace github.com/deepmap/oapi-codegen/v2 => ../

require (
github.com/deepmap/oapi-codegen/v2 v2.0.0-00010101000000-000000000000
github.com/getkin/kin-openapi v0.118.0
github.com/getkin/kin-openapi v0.122.0
github.com/gin-gonic/gin v1.9.1
github.com/go-chi/chi/v5 v5.0.10
github.com/gofiber/fiber/v2 v2.49.1
github.com/gorilla/mux v1.8.0
github.com/kataras/iris/v12 v12.2.6-0.20230908161203-24ba4e8933b9
github.com/labstack/echo/v4 v4.11.1
github.com/labstack/echo/v4 v4.11.3
github.com/lestrrat-go/jwx v1.2.26
github.com/oapi-codegen/echo-middleware v1.0.1
github.com/oapi-codegen/fiber-middleware v1.0.1
github.com/oapi-codegen/gin-middleware v1.0.1
github.com/oapi-codegen/iris-middleware v1.0.4
github.com/oapi-codegen/nethttp-middleware v1.0.1
github.com/oapi-codegen/runtime v1.0.0
github.com/oapi-codegen/runtime v1.1.0
github.com/oapi-codegen/testutil v1.0.0
github.com/stretchr/testify v1.8.4
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
Expand Down Expand Up @@ -52,7 +52,7 @@ require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/gomarkdown/markdown v0.0.0-20230716120725-531d2d74bc12 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
github.com/iris-contrib/schema v0.0.6 // indirect
Expand Down Expand Up @@ -102,11 +102,11 @@ require (
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/yosssi/ace v0.0.5 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.12.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
Expand Down
Loading

0 comments on commit 43c9ded

Please sign in to comment.