Skip to content

Commit

Permalink
Merge pull request #6 from markruler/add/example-value
Browse files Browse the repository at this point in the history
Converting example value
  • Loading branch information
markruler authored Apr 2, 2021
2 parents 3ea1589 + f90f86d commit 66ca5f7
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 29 deletions.
7 changes: 6 additions & 1 deletion xlsx/simple/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ func (simple *Simple) CreateAPISheet(path, method string, operation *spec.Operat
xl.File.NewSheet(xl.WorkSheetName)

xl.Context.Row = 1

simple.setAPISheetHeader(path, method, operation)
simple.setAPISheetRequest(operation)

if err = simple.setAPISheetRequest(operation); err != nil {
return err
}

if err = simple.setAPISheetResponse(operation); err != nil {
return err
}
Expand Down
13 changes: 10 additions & 3 deletions xlsx/simple/api_request.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package simple

import (
"encoding/json"
"fmt"
"reflect"

"github.com/go-openapi/spec"
"github.com/markruler/swage/parser"
)

func (simple *Simple) setAPISheetRequest(operation *spec.Operation) {
func (simple *Simple) setAPISheetRequest(operation *spec.Operation) error {
xl := simple.xl
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), "REQUEST")
xl.File.MergeCell(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row))
Expand All @@ -27,15 +28,20 @@ func (simple *Simple) setAPISheetRequest(operation *spec.Operation) {
xl.Context.Row++

for _, param := range operation.Parameters {
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "F", xl.Context.Row), xl.Style.Center)
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "E", xl.Context.Row), xl.Style.Center)
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "F", xl.Context.Row), fmt.Sprintf("%s%d", "F", xl.Context.Row), xl.Style.Left)

if !reflect.DeepEqual(param.Ref, spec.Ref{}) {
param = *simple.parameterFromRef(param.Ref)
}

simple.checkRequired(param.Required)

simple.setCellWithSchema(param.Name, param.In, param.Type, param.Description)
b, err := json.MarshalIndent(param.Example, "", " ")
if err != nil {
return err
}
simple.setCellWithSchema(param.Name, param.In, param.Type, string(b), param.Description)

if param.Items != nil && param.Items.Enum != nil {
xl.File.SetCellStr(xl.WorkSheetName, fmt.Sprintf("%s%d", "E", xl.Context.Row), parser.Enum2string(param.Items.Enum...))
Expand All @@ -52,4 +58,5 @@ func (simple *Simple) setAPISheetRequest(operation *spec.Operation) {
xl.Context.Row++
}
xl.Context.Row++
return nil
}
91 changes: 91 additions & 0 deletions xlsx/simple/api_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,94 @@ func TestParameterSchemaItemsWithoutRef(t *testing.T) {
assert.Equal(t, "array", row[12][3])
assert.Equal(t, "Status values that need to be considered for filter", row[12][6])
}

// docker.v1.41.json
func TestRequestDefinitionExample(t *testing.T) {
simple := New()
xl := simple.GetExcel()
xl.SwaggerSpec = &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Definitions: spec.Definitions{
"Port": {
SchemaProps: spec.SchemaProps{
Type: spec.StringOrArray{"object"},
Description: "An open port on a container",
Required: []string{"PrivatePort", "Type"},
Properties: spec.SchemaProperties{
"IP": {
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
Format: "ip-address",
Description: "Host IP address that the container's port is mapped to",
},
},
"PrivatePort": {
SchemaProps: spec.SchemaProps{
Type: []string{"integer"},
Format: "uint16",
Description: "Port on the container",
},
},
"PublicPort": {
SchemaProps: spec.SchemaProps{
Type: spec.StringOrArray{"integer"},
Format: "uint16",
Description: "Port exposed on the host",
},
},
"Type": {
SchemaProps: spec.SchemaProps{
Type: spec.StringOrArray{"string"},
Enum: []interface{}{"tcp", "udp", "sctp"},
},
},
},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: map[string]interface{}{
"PrivatePort": 8080,
"PublicPort": 80,
"Type": "tcp",
},
},
},
},
},
}
err := simple.CreateAPISheet("/containers/json", "get", &spec.Operation{
OperationProps: spec.OperationProps{
Tags: []string{"Container"},
Summary: "List containers",
Description: "Returns a list of containers. For details on the format, see the\n[inspect endpoint](#operation/ContainerInspect).\n\nNote that it uses a different, smaller representation of a container\nthan inspecting a single container. For example, the list of linked\ncontainers is not propagated .\n",
ID: "ContainerList",
Produces: []string{"application/json"},
Parameters: []spec.Parameter{
{
ParamProps: spec.ParamProps{
Name: "port",
In: "body",
Description: "Container port",
Required: true,
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.MustCreateRef("#/definitions/Port"),
},
},
},
},
},
Responses: &spec.Responses{},
},
}, nil, 1)
assert.NoError(t, err)
row, err := xl.File.GetRows("1")
assert.NoError(t, err)

assert.Equal(t, "O", row[12][0])
assert.Equal(t, "Port", row[12][1])
assert.Equal(t, "body", row[12][2])
assert.Equal(t, "object", row[12][3])
assert.Equal(t, "", row[12][4])
assert.Equal(t, "{\n \"PrivatePort\": 8080,\n \"PublicPort\": 80,\n \"Type\": \"tcp\"\n}", row[12][5])
assert.Equal(t, "Container port", row[12][6])
}
15 changes: 10 additions & 5 deletions xlsx/simple/api_response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simple

import (
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -44,17 +45,17 @@ func (simple *Simple) setAPISheetResponse(operation *spec.Operation) (err error)
return err
}
schemaName, _ := simple.definitionFromRef(responses.Default.Schema.Ref)
simple.setCellWithSchema(schemaName, "body", strings.Join(schema.Type, ","), responses.Default.Description)
simple.setCellWithSchema(schemaName, "body", strings.Join(schema.Type, ","), "", responses.Default.Description)
} else {
simple.setCellWithSchema("", "body", "string", responses.Default.Description)
simple.setCellWithSchema("", "body", "string", "", responses.Default.Description)
}
xl.Context.Row++
}

codes := parser.SortMap(responses.StatusCodeResponses)
for _, code := range codes {
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "F", xl.Context.Row), xl.Style.Center)
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "G", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row), xl.Style.Left)
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "A", xl.Context.Row), fmt.Sprintf("%s%d", "E", xl.Context.Row), xl.Style.Center)
xl.File.SetCellStyle(xl.WorkSheetName, fmt.Sprintf("%s%d", "F", xl.Context.Row), fmt.Sprintf("%s%d", "G", xl.Context.Row), xl.Style.Left)

icode, err := strconv.Atoi(code)
if err != nil {
Expand All @@ -70,7 +71,11 @@ func (simple *Simple) setAPISheetResponse(operation *spec.Operation) (err error)
}

for headerKey, header := range response.Headers {
simple.setCellWithSchema(headerKey, "header", header.Type, header.Description)
b, err := json.MarshalIndent(header.Example, "", " ")
if err != nil {
return err
}
simple.setCellWithSchema(headerKey, "header", header.Type, string(b), header.Description)
}

if response.Schema == nil {
Expand Down
32 changes: 27 additions & 5 deletions xlsx/simple/api_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestResponseWithoutSchema(t *testing.T) {
func TestResponseSchemaWithRef(t *testing.T) {
simple := New()
xl := simple.GetExcel()
var err error

xl.SwaggerSpec = &spec.Swagger{
SwaggerProps: spec.SwaggerProps{
Definitions: spec.Definitions{
Expand All @@ -137,10 +137,16 @@ func TestResponseSchemaWithRef(t *testing.T) {
},
},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: map[string]interface{}{
"message": "Something went wrong.",
},
},
},
},
},
}
var err error
err = simple.CreateAPISheet("", "", &spec.Operation{
OperationProps: spec.OperationProps{
Responses: &spec.Responses{
Expand Down Expand Up @@ -181,10 +187,11 @@ func TestResponseSchemaWithRef(t *testing.T) {
},
409: {
ResponseProps: spec.ResponseProps{
Description: "conflict",
Description: "indicates a request conflict with current state of the target resource.",
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.MustCreateRef("#/definitions/ErrorResponse"),
Title: "Conflict",
Type: spec.StringOrArray{"body"},
},
SwaggerSchemaProps: spec.SwaggerSchemaProps{
Example: map[string]map[string]interface{}{
Expand Down Expand Up @@ -214,30 +221,45 @@ func TestResponseSchemaWithRef(t *testing.T) {
assert.NoError(t, err)
row, err := xl.File.GetRows("1")
assert.NoError(t, err)

assert.Equal(t, "204", row[15][0])
assert.Equal(t, "", row[15][1])
assert.Equal(t, "", row[15][2])
assert.Equal(t, "", row[15][3])
assert.Equal(t, "", row[15][4])
assert.Equal(t, "", row[15][5])
assert.Equal(t, "no error", row[15][6])

assert.Equal(t, "400", row[16][0])
assert.Equal(t, "ErrorResponse", row[16][1])
assert.Equal(t, "body", row[16][2])
assert.Equal(t, "object", row[16][3])
assert.Equal(t, "", row[16][4])
assert.Equal(t, "{\n \"message\": \"Something went wrong.\"\n}", row[16][5])
assert.Equal(t, "bad parameter", row[16][6])

assert.Equal(t, "404", row[17][0])
assert.Equal(t, "ErrorResponse", row[17][1])
assert.Equal(t, "body", row[17][2])
assert.Equal(t, "object", row[17][3])
assert.Equal(t, "", row[17][4])
assert.Equal(t, "{\n \"message\": \"Something went wrong.\"\n}", row[17][5])
assert.Equal(t, "bad parameter", row[17][6])

assert.Equal(t, "409", row[18][0])
assert.Equal(t, "ErrorResponse", row[18][1])
assert.Equal(t, "Conflict", row[18][1])
assert.Equal(t, "body", row[18][2])
assert.Equal(t, "object", row[18][3])
assert.Equal(t, "conflict", row[18][6])
assert.Equal(t, "", row[18][4])
assert.Equal(t, "{\n \"application/json\": {\n \"message\": \"You cannot remove a running container: c2ada9df5af8. Stop the\\ncontainer before attempting removal or force remove\\n\"\n }\n}", row[18][5])
assert.Equal(t, "indicates a request conflict with current state of the target resource.", row[18][6])

assert.Equal(t, "500", row[19][0])
assert.Equal(t, "ErrorResponse", row[19][1])
assert.Equal(t, "body", row[19][2])
assert.Equal(t, "object", row[19][3])
assert.Equal(t, "", row[19][4])
assert.Equal(t, "{\n \"message\": \"Something went wrong.\"\n}", row[19][5])
assert.Equal(t, "server error", row[19][6])
}

Expand Down
Loading

0 comments on commit 66ca5f7

Please sign in to comment.