Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# editors
.idea
.vscode

/build
doc.json
1 change: 0 additions & 1 deletion internal/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type generatorConfig struct {
docVersion string
pathPrefix string
format string
verbose bool
}

type Option func(config *generatorConfig) error
Expand Down
14 changes: 9 additions & 5 deletions internal/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestGenerator(t *testing.T) {
name: "GetPet",
input: "GetPetRequest",
output: "GetPetResponse",
desc: "GetPet returns details about a pet\nIt accepts a pet id as an input and returns back the matching pet object\nreq-example: { \"pet_id\": \"123\" }\nreq-example: { \"pet_id\": \"456\" }\nres-example: { \"pet\": {\"name\": \"toby\"} }",
desc: "GetPet returns details about a pet\nIt accepts a pet id as an input and returns back the matching pet object",
},
}
messages := []ProtoMessage{
Expand Down Expand Up @@ -127,6 +127,10 @@ func TestGenerator(t *testing.T) {
itemsRef: "#/components/schemas/pet.v1.Vet",
itemsType: "object",
},
{
name: "mappings",
fieldType: "object",
},
},
},
}
Expand All @@ -139,16 +143,16 @@ func TestGenerator(t *testing.T) {
t.Errorf("%s: missing rpc %q", pathName, rpc.name)
}

if path.Description != rpc.desc {
t.Errorf("%s: expected desc %q but got %q", pathName, rpc.desc, path.Description)
}

post := path.Post
if post == nil {
t.Errorf("%s: missing post", pathName)
continue
}

if post.Description != rpc.desc {
t.Errorf("%s: expected desc %q but got %q", pathName, rpc.desc, post.Description)
}

if post.Summary != rpc.name {
t.Errorf("%s: expected summary %q but got %q", pathName, rpc.name, post.Summary)
}
Expand Down
28 changes: 24 additions & 4 deletions internal/generator/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (gen *generator) Message(msg *proto.Message) {
gen.addField(schemaProps, val.Field, false)
case *proto.MapField:
//logger.logd("proto.MapField")
gen.addField(schemaProps, val.Field, false)
gen.addMap(schemaProps, val)
case *proto.NormalField:
//logger.logd("proto.NormalField %q %q", val.Field.Type, val.Field.Name)
gen.addField(schemaProps, val.Field, val.Repeated)
Expand All @@ -209,6 +209,26 @@ func (gen *generator) Message(msg *proto.Message) {
}
}

func (gen *generator) addMap(schemaPropsV3 openapi3.Schemas, field *proto.MapField) {
fieldDescription := description(field.Comment)
fieldName := field.Name

addProps := openapi3.Schemas{}
gen.addField(addProps, field.Field, false)

fieldSchemaV3 := openapi3.SchemaRef{
Value: &openapi3.Schema{
Description: fieldDescription,
Type: "object",
AdditionalProperties: openapi3.AdditionalProperties{
Schema: addProps[fieldName],
},
},
}

schemaPropsV3[fieldName] = &fieldSchemaV3
}

func (gen *generator) addField(schemaPropsV3 openapi3.Schemas, field *proto.Field, repeated bool) {
fieldDescription := description(field.Comment)
fieldName := field.Name
Expand Down Expand Up @@ -520,7 +540,7 @@ func parseComment(comment *proto.Comment) (string, []map[string]interface{}, []m
}
reqExamples := []map[string]interface{}{}
respExamples := []map[string]interface{}{}
message := ""
var message []string
for _, line := range comment.Lines {
line = strings.TrimLeftFunc(line, unicode.IsSpace)
if strings.HasPrefix(line, "req-example:") {
Expand All @@ -538,8 +558,8 @@ func parseComment(comment *proto.Comment) (string, []map[string]interface{}, []m
}
respExamples = append(respExamples, example)
} else {
message = fmt.Sprintf("%s\n%s", message, line)
message = append(message, line)
}
}
return message, reqExamples, respExamples, nil
return strings.Join(message, "\n"), reqExamples, respExamples, nil
}
Loading