Skip to content

Commit

Permalink
Add fallback for old configuration style
Browse files Browse the repository at this point in the history
As a means to avoid the breaking changes we introduced in v1.11.0, where
the new-style configuration was made the default, we can tweak our
heuristic for how we work out what configuration is in use by only using
the new-style config if the new `--config` flag is used.

This change in itself is non-breaking, and makes it possible to continue
using the old configuration arguments, as long as you don't provide the
new `--config` flag.

Similar to thoughts in oapi-codegen#629, but does not require a revert.

Closes oapi-codegen#629.
  • Loading branch information
Jamie Tanna committed Jul 29, 2022
1 parent b9782fb commit d4fd68b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,12 @@ output-options:
Have a look at [`cmd/oapi-codegen/oapi-codegen.go`](https://github.com/deepmap/oapi-codegen/blob/master/cmd/oapi-codegen/oapi-codegen.go#L48)
to see all the fields on the configuration structure.

**Note:** the old configuration options, present in pre-1.11.0 releases will work again:

```sh
oapi-codegen --package=petshop --generate="types,client,server,spec" -alias-types petshop-expanded.yaml
```

### Import Mappings

OpenAPI specifications may contain references to other OpenAPI specifications,
Expand Down
23 changes: 14 additions & 9 deletions cmd/oapi-codegen/oapi-codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ func errExit(format string, args ...interface{}) {
}

var (
flagOutputFile string
flagConfigFile string
flagOldConfigStyle bool
flagOutputConfig bool
flagPrintVersion bool
flagPackageName string
isOldConfigStyle bool

flagOutputFile string
flagConfigFile string
flagOutputConfig bool
flagPrintVersion bool
flagPackageName string

// The options below are deprecated, and they will be removed in a future
// release. Please use the new config file format.
Expand Down Expand Up @@ -77,7 +78,6 @@ type oldConfiguration struct {

func main() {
flag.StringVar(&flagOutputFile, "o", "", "Where to output generated code, stdout is default")
flag.BoolVar(&flagOldConfigStyle, "old-config-style", false, "whether to use the older style config file format")
flag.BoolVar(&flagOutputConfig, "output-config", false, "when true, outputs a configuration file for oapi-codegen using current settings")
flag.StringVar(&flagConfigFile, "config", "", "a YAML config file that controls oapi-codegen behavior")
flag.BoolVar(&flagPrintVersion, "version", false, "when specified, print version and exit")
Expand Down Expand Up @@ -113,8 +113,13 @@ func main() {
os.Exit(1)
}

// fallback to the old config style if we don't receive the new `--config` flag
if flagConfigFile == "" {
isOldConfigStyle = true
}

var opts configuration
if !flagOldConfigStyle {
if !isOldConfigStyle {
// We simply read the configuration from disk.
if flagConfigFile != "" {
buf, err := ioutil.ReadFile(flagConfigFile)
Expand Down Expand Up @@ -258,7 +263,7 @@ func updateConfigFromFlags(cfg configuration) (configuration, error) {

if len(unsupportedFlags) > 0 {
return configuration{}, fmt.Errorf("flags %s aren't supported in "+
"new config style, please use -old-config-style or update your configuration ",
"new config style, please remove the --config flag or update your configuration ",
strings.Join(unsupportedFlags, ", "))
}

Expand Down
2 changes: 1 addition & 1 deletion internal/test/client/doc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package client

//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --old-config-style --package=client -o client.gen.go client.yaml
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --package=client -o client.gen.go client.yaml
2 changes: 1 addition & 1 deletion internal/test/issues/issue-579/gen.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package issue_579

//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --old-config-style --package=issue_579 --generate=types,skip-prune --alias-types -o issue.gen.go spec.yaml
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen --package=issue_579 --generate=types,skip-prune --alias-types -o issue.gen.go spec.yaml

0 comments on commit d4fd68b

Please sign in to comment.