Skip to content

Commit

Permalink
Fix x-go-type-import code generation (deepmap#682)
Browse files Browse the repository at this point in the history
* Fix x-go-type-import statements

* Add better test coverage for x-go-type-import
  • Loading branch information
wes-mil committed Jul 25, 2022
1 parent 03d2425 commit 1520c96
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 10 deletions.
25 changes: 17 additions & 8 deletions pkg/codegen/codegen.go
Expand Up @@ -819,7 +819,6 @@ func GetTypeDefinitionsImports(swagger *openapi3.T, excludeSchemas []string) (ma
}

func GetSchemaImports(schemas map[string]*openapi3.SchemaRef, excludeSchemas []string) (map[string]goImport, error) {
var err error
res := map[string]goImport{}
excludeSchemasMap := make(map[string]bool)
for _, schema := range excludeSchemas {
Expand All @@ -835,17 +834,20 @@ func GetSchemaImports(schemas map[string]*openapi3.SchemaRef, excludeSchemas []s
continue
}

res, err = GetImports(schema.Properties)
imprts, err := GetImports(schema.Properties)
if err != nil {
return nil, err
}

for s, gi := range imprts {
res[s] = gi
}
}
return res, nil
}

func GetRequestBodiesImports(bodies map[string]*openapi3.RequestBodyRef) (map[string]goImport, error) {
var res map[string]goImport
var err error
res := map[string]goImport{}
for _, requestBodyName := range SortedRequestBodyKeys(bodies) {
requestBodyRef := bodies[requestBodyName]
response := requestBodyRef.Value
Expand All @@ -856,18 +858,21 @@ func GetRequestBodiesImports(bodies map[string]*openapi3.RequestBodyRef) (map[st
continue
}

res, err = GetImports(schema.Value.Properties)
imprts, err := GetImports(schema.Value.Properties)
if err != nil {
return nil, err
}

for s, gi := range imprts {
res[s] = gi
}
}
}
return res, nil
}

func GetResponsesImports(responses map[string]*openapi3.ResponseRef) (map[string]goImport, error) {
var res map[string]goImport
var err error
res := map[string]goImport{}
for _, responseName := range SortedResponsesKeys(responses) {
responseOrRef := responses[responseName]
response := responseOrRef.Value
Expand All @@ -878,10 +883,14 @@ func GetResponsesImports(responses map[string]*openapi3.ResponseRef) (map[string
continue
}

res, err = GetImports(schema.Value.Properties)
imprts, err := GetImports(schema.Value.Properties)
if err != nil {
return nil, err
}

for s, gi := range imprts {
res[s] = gi
}
}
}
return res, nil
Expand Down
9 changes: 9 additions & 0 deletions pkg/codegen/codegen_test.go
Expand Up @@ -220,6 +220,15 @@ func TestXGoTypeImport(t *testing.T) {
// Check generated struct
assert.Contains(t, code, "type Pet struct {\n\tAge myuuid.UUID `json:\"age\"`\n}")

// Check import
assert.Contains(t, code, `github.com/CavernaTechnologies/pgext`)

// Check generated struct
assert.Contains(t, code, "type Person struct {\n\tAge pgext.Puint `json:\"age\"`\n}")

// Check generated struct
assert.Contains(t, code, "type Car struct {\n\tAge int `json:\"age\"`\n}")

// Make sure the generated code is valid:
checkLint(t, "test.gen.go", []byte(code))

Expand Down
59 changes: 57 additions & 2 deletions pkg/codegen/test_specs/x-go-type-import-pet.yaml
Expand Up @@ -28,12 +28,53 @@ paths:
type: integer
format: int64
responses:
'200':
"200":
description: pet response
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
$ref: "#/components/schemas/Pet"
/person/{id}:
get:
summary: Returns a person by ID
description: Returns a person based on a single ID
operationId: findPersonByID
parameters:
- name: id
in: path
description: ID of person to fetch
required: true
schema:
type: integer
format: int64
responses:
"200":
description: person response
content:
application/json:
schema:
$ref: "#/components/schemas/Person"
/car/{id}:
get:
summary: Returns a car by ID
description: Returns a car based on a single ID
operationId: findCarByID
parameters:
- name: id
in: path
description: ID of car to fetch
required: true
schema:
type: integer
format: int64
responses:
"200":
description: car response
content:
application/json:
schema:
$ref: "#/components/schemas/Car"

components:
schemas:
Pet:
Expand All @@ -45,3 +86,17 @@ components:
name: myuuid
required:
- age
Person:
properties:
age:
x-go-type: pgext.Puint
x-go-type-import:
path: github.com/CavernaTechnologies/pgext
required:
- age
Car:
properties:
age:
type: integer
required:
- age

0 comments on commit 1520c96

Please sign in to comment.