Skip to content

Commit

Permalink
fix(*): handle the custom Destination in API docs and client generati…
Browse files Browse the repository at this point in the history
…on (#413)

Co-authored-by: iawia002 <[email protected]>
  • Loading branch information
caicloud-bot and iawia002 authored Dec 9, 2020
1 parent 5714d9a commit 6f5ab44
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
11 changes: 11 additions & 0 deletions rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,17 @@ func (r *Request) Meta(value *map[string]string) *Request {
return r
}

type topRPCResponse struct {
Result interface{} `json:"Result"`
}

// TOPRPCData sets TOP RPC style body result. The value must be a pointer.
// For internal uses.
func (r *Request) TOPRPCData(value interface{}) *Request {
r.data = &topRPCResponse{Result: value}
return r
}

// Data sets body result. value must be a pointer.
func (r *Request) Data(value interface{}) *Request {
r.data = value
Expand Down
2 changes: 1 addition & 1 deletion utils/generators/golang/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func (h *helper) Functions() ([]function, []string) {
fn.Parameters = append(fn.Parameters, p)
}
for _, result := range def.Results {
if result.Destination == definition.Error {
if strings.Contains(string(result.Destination), string(definition.Error)) {
// Ignore errors
continue
}
Expand Down
17 changes: 16 additions & 1 deletion utils/generators/swagger/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,24 @@ func (g *Generator) enum(typ *api.Type) []spec.Parameter {
return results
}

func parseDestination(d definition.Destination) definition.Destination {
switch {
// for the custom Destination
case strings.Contains(string(d), string(definition.Meta)):
return definition.Meta
case strings.Contains(string(d), string(definition.Data)):
return definition.Data
case strings.Contains(string(d), string(definition.Error)):
return definition.Error
default:
return d
}
}

func (g *Generator) generateResponse(results []api.Result, examples []api.Example) *spec.Response {
response := &spec.Response{}
for _, result := range results {
switch g.destinationMapping[result.Destination] {
switch g.destinationMapping[parseDestination(result.Destination)] {
case "body":
response.Description = g.escapeNewline(result.Description)
schema := g.schemaForTypeName(result.Type)
Expand All @@ -609,6 +623,7 @@ func (g *Generator) generateResponse(results []api.Result, examples []api.Exampl
}
return response
}

func (g *Generator) escapeNewline(content string) string {
return strings.Replace(strings.TrimSpace(content), "\n", "<br/>", -1)
}
Expand Down

0 comments on commit 6f5ab44

Please sign in to comment.