Repository: deepmap/oapi-codegen
Pull request #217 (2020-08-16 04:37:17)

docs: ✏️ Add the description for "x-go-type" into README
I would like to add a description for "x-go-type" into README.md.
The property was merged by #212.

The description to be added is as follows.
![image](https://user-images.githubusercontent.com/2452581/90326562-b450ce00-dfc4-11ea-9e56-4302cb850525.png)


pushkargr: @deepmap-marcinr Please release this change. 

Patch:
diff --git a/README.md b/README.md
index bb36ca9..c365811 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
@@ -419,6 +419,15 @@ which help you to use the various OpenAPI 3 Authentication mechanism.
     }
 ```
 
+## Extensions
+
+`oapi-codegen` supports the following extended properties:
+
+- `x-go-type`: specifies Go type name. It allows you to specify the type name for a schema, and
+ will override any default value. This extended property isn't supported in all parts of
+ OpenAPI, so please refer to the spec as to where it's allowed. Swagger validation tools will
+ flag incorrect usage of this property.
+
 ## Using `oapi-codegen`
 
 The default options for `oapi-codegen` will generate everything; client, server,
@@ -456,10 +465,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 +479,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 +487,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/schema.go b/pkg/codegen/schema.go
index cbee286..ebca031 100644
--- a/pkg/codegen/schema.go
+++ b/pkg/codegen/schema.go
@@ -241,8 +241,12 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
 			// We default to int if format doesn't ask for something else.
 			if f == "int64" {
 				outSchema.GoType = "int64"
+			} else if f == "uint64" {
+				outSchema.GoType = "uint64"
 			} else if f == "int32" {
 				outSchema.GoType = "int32"
+			} else if f == "uint32" {
+				outSchema.GoType = "uint32"
 			} else if f == "" {
 				outSchema.GoType = "int"
 			} else {
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")
+		}
+	}
+}