Skip to content

Commit

Permalink
Better integration test coverage, bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgtaylor committed Oct 8, 2018
1 parent 3631922 commit b2dc62d
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 75 deletions.
96 changes: 50 additions & 46 deletions bindata.go

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

14 changes: 13 additions & 1 deletion example-cli/openapi.go

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

8 changes: 8 additions & 0 deletions example-cli/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ paths:
post:
operationId: echo
description: Echo back body with the same content type.
parameters:
- name: q
x-cli-name: echo-query
in: query
- name: x-request-id
in: header
schema:
type: string
requestBody:
content:
application/json:
Expand Down
51 changes: 41 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
// Param describes an OpenAPI parameter (path, query, header, etc)
type Param struct {
Name string
CLIName string
GoName string
Description string
In string
Expand Down Expand Up @@ -62,8 +63,15 @@ type Server struct {
// TODO: handle server parameters
}

// Imports describe optional imports based on features in use.
type Imports struct {
Fmt bool
Strings bool
}

// OpenAPI describes an API
type OpenAPI struct {
Imports Imports
Name string
GoName string
Title string
Expand All @@ -77,12 +85,12 @@ type OpenAPI struct {
func ProcessAPI(shortName string, api *openapi3.Swagger) *OpenAPI {
apiName := shortName
if api.Info.Extensions[ExtName] != nil {
apiName = api.Info.Extensions[ExtName].(string)
apiName = extStr(api.Info.Extensions[ExtName])
}

apiDescription := api.Info.Description
if api.Info.Extensions[ExtDescription] != nil {
apiDescription = api.Info.Extensions[ExtDescription].(string)
apiDescription = extStr(api.Info.Extensions[ExtDescription])
}

result := &OpenAPI{
Expand Down Expand Up @@ -121,7 +129,7 @@ func ProcessAPI(shortName string, api *openapi3.Swagger) *OpenAPI {

name := operation.OperationID
if operation.Extensions[ExtName] != nil {
name = operation.Extensions[ExtName].(string)
name = extStr(operation.Extensions[ExtName])
}

var aliases []string
Expand All @@ -142,7 +150,7 @@ func ProcessAPI(shortName string, api *openapi3.Swagger) *OpenAPI {

description := operation.Description
if operation.Extensions[ExtDescription] != nil {
description = operation.Extensions[ExtDescription].(string)
description = extStr(operation.Extensions[ExtDescription])
}

reqMt, reqSchema, reqExamples := getRequestInfo(operation)
Expand Down Expand Up @@ -202,12 +210,34 @@ func ProcessAPI(shortName string, api *openapi3.Swagger) *OpenAPI {
}

result.Operations = append(result.Operations, o)

for _, p := range params {
if p.In == "path" {
result.Imports.Strings = true
}
}

for _, p := range optionalParams {
if p.In == "query" || p.In == "header" {
result.Imports.Fmt = true
}
}
}
}

return result
}

// extStr returns the string value of an OpenAPI extension stored as a JSON
// raw message.
func extStr(i interface{}) (decoded string) {
if err := json.Unmarshal(i.(json.RawMessage), &decoded); err != nil {
panic(err)
}

return
}

func toGoName(input string, public bool) string {
transformed := strings.Replace(input, "-", " ", -1)
transformed = strings.Replace(transformed, "_", " ", -1)
Expand Down Expand Up @@ -256,7 +286,7 @@ func getParams(path *openapi3.PathItem, httpMethod string) []*Param {
if p.Value != nil && p.Value.Extensions["x-cli-ignore"] == nil {
t := "string"
tn := "\"\""
if p.Value.Schema.Value != nil && p.Value.Schema.Value.Type != "" {
if p.Value.Schema != nil && p.Value.Schema.Value != nil && p.Value.Schema.Value.Type != "" {
switch p.Value.Schema.Value.Type {
case "boolean":
t = "bool"
Expand All @@ -270,19 +300,20 @@ func getParams(path *openapi3.PathItem, httpMethod string) []*Param {
}
}

name := p.Value.Name
cliName := slug(p.Value.Name)
if p.Value.Extensions[ExtName] != nil {
name = p.Value.Extensions[ExtName].(string)
cliName = extStr(p.Value.Extensions[ExtName])
}

description := p.Value.Description
if p.Value.Extensions[ExtDescription] != nil {
description = p.Value.Extensions[ExtDescription].(string)
description = extStr(p.Value.Extensions[ExtDescription])
}

allParams = append(allParams, &Param{
Name: name,
GoName: toGoName("param "+name, false),
Name: p.Value.Name,
CLIName: cliName,
GoName: toGoName("param "+cliName, false),
Description: description,
In: p.Value.In,
Required: p.Value.Required,
Expand Down
26 changes: 22 additions & 4 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package main_test

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"testing"

"github.com/alecthomas/assert"
"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
Expand All @@ -33,7 +35,22 @@ func TestMain(m *testing.M) {
w.Header().Add("Content-Type", ct)
}

w.Write(body)
// For the test, add the param values to the echoed response.
var decoded map[string]interface{}
json.Unmarshal(body, &decoded)

q := r.URL.Query().Get("q")
if q != "" {
decoded["q"] = q
}

rid := r.Header.Get("X-Request-ID")
if rid != "" {
decoded["request-id"] = rid
}

marshalled, _ := json.Marshal(decoded)
w.Write(marshalled)
})

go func() {
Expand All @@ -46,10 +63,11 @@ func TestMain(m *testing.M) {

func TestEchoSuccess(t *testing.T) {
// Call the precompiled executable CLI to hit our test server.
out, err := exec.Command("sh", "-c", "example-cli echo hello: world").CombinedOutput()
out, err := exec.Command("sh", "-c", "example-cli echo hello: world --echo-query=foo --x-request-id bar").CombinedOutput()
if err != nil {
fmt.Println(string(out))
panic(err)
}

assert.Equal(t, "{\n \"hello\": \"world\"\n}\n\n", string(out))
assert.JSONEq(t, "{\"hello\": \"world\", \"q\": \"foo\", \"request-id\": \"bar\"}", string(out))
}
Loading

0 comments on commit b2dc62d

Please sign in to comment.