Repository: deepmap/oapi-codegen
Pull request #216 (2020-08-13 12:09:37)

Allow giving URLs to oapi-codegen
At the moment I have to download a file before giving it to oapi-codegen, this helps wit that.


Patch:
diff --git a/README.md b/README.md
index bb36ca9..fcdea9e 100644
--- a/README.md
+++ b/README.md
@@ -246,7 +246,7 @@ func (a NewPet) MarshalJSON() ([]byte, error) {...}w
 There are many special cases for `additionalProperties`, such as having to
 define types for inner fields which themselves support additionalProperties, and
 all of them are tested via the `internal/test/components` schemas and tests. Please
-look through those tests for more usage examples. 
+look through those tests for more usage examples.
 
 ## Generated Client Boilerplate
 
@@ -319,7 +319,7 @@ The Client object above is fairly flexible, since you can pass in your own
 headers. In our middleware stack, we annotate the context with additional
 information such as the request ID and function tracing information, and we
 use the callback to propagate that information into the request headers. Still, we
-can't foresee all possible usages, so those functions call through to helper 
+can't foresee all possible usages, so those functions call through to helper
 functions which create requests. In the case of the pet store, we have:
 
 ```go
@@ -456,10 +456,10 @@ To generate a server that only handles `admin` paths, use the argument
 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-tags=Pet,NewPet`
+a comma separated list of schema names. For instance, `--exclude-schemas=Pet,NewPet`
 will exclude from generation schemas `Pet` and `NewPet`. This allow to have a
-in the same package a manually defined structure or interface and refer to it 
-in the openapi spec. 
+in the same package a manually defined structure or interface and refer to it
+in the openapi spec.
 
 ### Import Mappings
 
@@ -470,7 +470,7 @@ Go code.
 An external reference looks like this:
 
     $ref: ./some_spec.yaml#/components/schemas/Type
-    
+
 We assume that you have already generated the boilerplate code for `./some_spec.yaml`
 using `oapi-codegen`, and you have a package which contains the generated code,
 let's call it `github.com/deepmap/some-package`. You need to tell `oapi-codegen` that
@@ -478,11 +478,11 @@ let's call it `github.com/deepmap/some-package`. You need to tell `oapi-codegen`
 this command line argument:
 
     -import-mapping=./some_spec.yaml:github.com/deepmap/some-package
-    
+
 This tells us that in order to resolve references generated from `some_spec.yaml` we
 need to import `github.com/deepmap/some-package`. You may specify multiple mappings
 by comma separating them in the form `key1:value1,key2:value2`.
-    
+
 ## What's missing or incomplete
 
 This code is still young, and not complete, since we're filling it in as we
diff --git a/pkg/codegen/utils.go b/pkg/codegen/utils.go
index 6c577d9..6731363 100644
--- a/pkg/codegen/utils.go
+++ b/pkg/codegen/utils.go
@@ -22,7 +22,6 @@ import (
 	"unicode"
 
 	"github.com/getkin/kin-openapi/openapi3"
-	"github.com/pkg/errors"
 )
 
 var pathParamRE *regexp.Regexp
@@ -204,8 +203,8 @@ func StringInArray(str string, array []string) bool {
 func RefPathToGoType(refPath string) (string, error) {
 	if refPath[0] == '#' {
 		pathParts := strings.Split(refPath, "/")
-		if len(pathParts) != 4 {
-			return "", errors.New("Parameter nesting is deeper than supported")
+		if depth := len(pathParts); depth != 4 {
+			return "", fmt.Errorf("Parameter nesting is deeper than supported: %s has %d", refPath, depth)
 		}
 		return SchemaNameToTypeName(pathParts[3]), nil
 	}
diff --git a/pkg/util/loader.go b/pkg/util/loader.go
index 56b0297..4825620 100644
--- a/pkg/util/loader.go
+++ b/pkg/util/loader.go
@@ -1,13 +1,20 @@
 package util
 
 import (
+	"net/url"
+
 	"github.com/getkin/kin-openapi/openapi3"
 )
 
 func LoadSwagger(filePath string) (swagger *openapi3.Swagger, err error) {
+
 	loader := openapi3.NewSwaggerLoader()
 	loader.IsExternalRefsAllowed = true
-	swagger, err = loader.LoadSwaggerFromFile(filePath)
-	return
-}
 
+	u, err := url.Parse(filePath)
+	if err == nil && u.Scheme != "" && u.Host != "" {
+		return loader.LoadSwaggerFromURI(u)
+	} else {
+		return loader.LoadSwaggerFromFile(filePath)
+	}
+}


Test patch:
diff --git a/cmd/oapi-codegen/oapi-codegen_test.go b/cmd/oapi-codegen/oapi-codegen_test.go
new file mode 100644
index 0000000..06992d9
--- /dev/null
+++ b/cmd/oapi-codegen/oapi-codegen_test.go
@@ -0,0 +1,26 @@
+package main
+
+import (
+	"testing"
+
+	"github.com/deepmap/oapi-codegen/pkg/util"
+)
+
+func TestLoader(t *testing.T) {
+
+	paths := []string{
+		"../../examples/petstore-expanded/petstore-expanded.yaml",
+		"https://petstore3.swagger.io/api/v3/openapi.json",
+	}
+
+	for _, v := range paths {
+
+		swagger, err := util.LoadSwagger(v)
+		if err != nil {
+			t.Error(err)
+		}
+		if swagger == nil || swagger.Info == nil || swagger.Info.Version == "" {
+			t.Error("missing data")
+		}
+	}
+}