Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions cmd/oapi-codegen/oapi-codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path"
"path/filepath"
"runtime/debug"
"strconv"
"strings"

"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -281,20 +282,41 @@ func main() {
return
}

swagger, err := util.LoadSwagger(flag.Arg(0))
spec, err := util.LoadOpenAPI(flag.Arg(0))
if err != nil {
errExit("error loading swagger spec in %s\n: %s\n", flag.Arg(0), err)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be this?

Suggested change
errExit("error loading swagger spec in %s\n: %s\n", flag.Arg(0), err)
errExit("error loading OpenAPI spec in %s\n: %s\n", flag.Arg(0), err)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunately a wider issue #672

}

if strings.HasPrefix(swagger.OpenAPI, "3.1.") {
// In older versions of OpenAPI (aka, Swagger), the `openapi` property won't be present in the top level, so we should
// refuse to load those. We can identify them by the OpenAPI version being an empty string.
if spec.OpenAPI == "" {
errExit("'openapi' property not found in parsed spec. You are likely loading an older Swagger file that's unsupported")
}

// Now, extract semver from the OpenAPI header.
versionParts := strings.Split(strings.TrimSpace(spec.OpenAPI), ".")
if len(versionParts) != 3 {
errExit("openapi property in spec is not valid semver: %s", spec.OpenAPI)
}
major := versionParts[0]
minor := versionParts[1]

if major != "3" {
errExit("only openapi specs with major version 3 are supported")
}
minorAsNum, err := strconv.ParseInt(minor, 10, 64)
if err != nil {
errExit("couldn't parse openapi minor version as integer: '%s'", minor)
}
if minorAsNum > 0 {
fmt.Println("WARNING: You are using an OpenAPI 3.1.x specification, which is not yet supported by oapi-codegen (https://github.com/deepmap/oapi-codegen/issues/373) and so some functionality may not be available. Until oapi-codegen supports OpenAPI 3.1, it is recommended to downgrade your spec to 3.0.x")
}

if len(noVCSVersionOverride) > 0 {
opts.Configuration.NoVCSVersionOverride = &noVCSVersionOverride
}

code, err := codegen.Generate(swagger, opts.Configuration)
code, err := codegen.Generate(spec, opts.Configuration)
if err != nil {
errExit("error generating code: %s\n", err)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/oapi-codegen/oapi-codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestLoader(t *testing.T) {

for _, v := range paths {

swagger, err := util.LoadSwagger(v)
swagger, err := util.LoadOpenAPI(v)
if err != nil {
t.Error(err)
}
Expand Down
1 change: 1 addition & 0 deletions examples/import-mapping/common/api.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
openapi: 3.0.0
components:
schemas:
User:
Expand Down
1 change: 1 addition & 0 deletions internal/test/extensions/x-order/spec.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
openapi: 3.0.0
components:
schemas:
DateInterval:
Expand Down
6 changes: 3 additions & 3 deletions internal/test/externalref/packageA/externalref.gen.go

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

3 changes: 2 additions & 1 deletion internal/test/externalref/packageA/spec.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
openapi: 3.0.0
components:
schemas:
ObjectA:
properties:
name:
type: string
object_b:
$ref: ../packageB/spec.yaml#/components/schemas/ObjectB
$ref: ../packageB/spec.yaml#/components/schemas/ObjectB
5 changes: 3 additions & 2 deletions internal/test/externalref/packageB/externalref.gen.go

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

1 change: 1 addition & 0 deletions internal/test/externalref/packageB/spec.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
openapi: 3.0.0
components:
schemas:
ObjectB:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
eopenapi: 3.0.2
openapi: 3.0.2
info:
version: "0.0.1"
paths:
Expand Down
1 change: 1 addition & 0 deletions internal/test/outputoptions/disabletypealiases/spec.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
openapi: 3.0.0
components:
schemas:
my_item:
Expand Down
6 changes: 3 additions & 3 deletions pkg/codegen/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestExtPropGoTypeSkipOptionalPointer(t *testing.T) {
},
}
spec := "test_specs/x-go-type-skip-optional-pointer.yaml"
swagger, err := util.LoadSwagger(spec)
swagger, err := util.LoadOpenAPI(spec)
require.NoError(t, err)

// Run our code generation:
Expand Down Expand Up @@ -134,7 +134,7 @@ func TestGoTypeImport(t *testing.T) {
},
}
spec := "test_specs/x-go-type-import-pet.yaml"
swagger, err := util.LoadSwagger(spec)
swagger, err := util.LoadOpenAPI(spec)
require.NoError(t, err)

// Run our code generation:
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestRemoteExternalReference(t *testing.T) {
},
}
spec := "test_specs/remote-external-reference.yaml"
swagger, err := util.LoadSwagger(spec)
swagger, err := util.LoadOpenAPI(spec)
require.NoError(t, err)

// Run our code generation:
Expand Down
12 changes: 9 additions & 3 deletions pkg/util/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ import (
"github.com/getkin/kin-openapi/openapi3"
)

func LoadSwagger(filePath string) (swagger *openapi3.T, err error) {
// Deprecated: LoadSwagger is deprecated as that name isn't ours to use. Call
// LoadOpenAPI instead.
func LoadSwagger(filePath string) (*openapi3.T, error) {
return LoadOpenAPI(filePath)
}

// LoadOpenAPI loads a local or remote OpenAPI spec
func LoadOpenAPI(filePath string) (*openapi3.T, error) {
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true

Expand All @@ -21,7 +27,7 @@ func LoadSwagger(filePath string) (swagger *openapi3.T, err error) {

// Deprecated: In kin-openapi v0.126.0 (https://github.com/getkin/kin-openapi/tree/v0.126.0?tab=readme-ov-file#v01260) the Circular Reference Counter functionality was removed, instead resolving all references with backtracking, to avoid needing to provide a limit to reference counts.
//
// This is now identital in method as `LoadSwagger`.
// This is now identital in method as `LoadOpenAPI`.
func LoadSwaggerWithCircularReferenceCount(filePath string, _ int) (swagger *openapi3.T, err error) {
return LoadSwagger(filePath)
return LoadOpenAPI(filePath)
}