Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Jess Frazelle <[email protected]>
  • Loading branch information
jessfraz committed Apr 11, 2024
1 parent af342aa commit c71ff96
Show file tree
Hide file tree
Showing 13 changed files with 4,389 additions and 804 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.32
v0.2.33
10 changes: 10 additions & 0 deletions client.go

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

17 changes: 17 additions & 0 deletions cmd/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,10 +269,23 @@ func (data *Data) generateMethod(doc *openapi3.T, method string, pathName string
// Now we can get the description since we have filled in everything else.
function.Description = function.getDescription(operation)

isMultiPart := false
if operation.RequestBody != nil {
for mediaType := range operation.RequestBody.Value.Content {
if strings.Contains(mediaType, "multipart/form-data") {
isMultiPart = true
break
}
}
}

exampleTemplatePath := "function-example.tmpl"
if _, ok := operation.Extensions["x-dropshot-websocket"]; ok {
exampleTemplatePath = "function-example-ws.tmpl"
}
if isMultiPart {
exampleTemplatePath = "function-example-multipart.tmpl"
}

// Build the example function.
example, err := templateToString(exampleTemplatePath, function)
Expand All @@ -285,6 +298,10 @@ func (data *Data) generateMethod(doc *openapi3.T, method string, pathName string
if _, ok := operation.Extensions["x-dropshot-websocket"]; ok {
templatePath = "websocket.tmpl"
}
// If its a multipart request, we need to use a different template.
if isMultiPart {
templatePath = "multipart.tmpl"
}

// Print the template for the function.
f, err := templateToString(templatePath, function)
Expand Down
22 changes: 22 additions & 0 deletions cmd/tmpl/function-example-multipart.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// {{.Description}}
func Example{{.Tag}}Service_{{.Name}}() {
client, err := {{.PackageName}}.NewClientFromEnv("your apps user agent")
if err != nil {
panic(err)
}

buf := new(bytes.Buffer)

{{if .Response}}
result, err := client.{{.Tag}}.{{.Name}}({{range .Args -}}{{.Example}},{{end -}} buf)
if err != nil {
panic(err)
}

fmt.Printf("%#v", result)
{{else}}
if err := client.{{.Tag}}.{{.Name}}({{range .Args -}}{{.Example}},{{end -}} buf); err != nil {
panic(err)
}
{{end}}
}
58 changes: 58 additions & 0 deletions cmd/tmpl/multipart.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// {{.Description}}
func (s *{{.Tag}}Service) {{.Name}}({{range .Args -}}{{.Name}} {{.Type}},{{end -}} body *bytes.Buffer) {{if .Response}}(*{{.Response.Type}}, error){{else}}error{{end}} {
// Create the url.
path := "{{.Path}}"
uri := resolveRelative(s.client.server, path)


// Create the request.
req, err := http.NewRequest("{{.Method}}", uri, body)
if err != nil {
return {{if .Response}}nil,{{end}} fmt.Errorf("error creating request: %v", err)
}

{{if .RequestBody}}
// Add our headers.
req.Header.Add("Content-Type", "{{.RequestBody.MediaType}}")
{{end}}

{{if .Args}}
// Add the parameters to the url.
if err := expandURL(req.URL, map[string]string{
{{range .Args -}}
"{{.Property}}": {{.ToString}},
{{end -}}
}); err != nil {
return {{if .Response}}nil,{{end}} fmt.Errorf("expanding URL with parameters failed: %v", err)
}
{{end}}

// Send the request.
resp, err := s.client.client.Do(req)
if err != nil {
return {{if .Response}}nil,{{end}} fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()

// Check the response.
if err := checkResponse(resp); err != nil {
return {{if .Response}}nil,{{end}} err
}

{{if .Response}}
// Decode the body from the response.
if resp.Body == nil {
return nil, errors.New("request returned an empty body in the response")
}
var decoded {{.Response.Type}}
if err := json.NewDecoder(resp.Body).Decode(&decoded); err != nil {
return nil, fmt.Errorf("error decoding response body: %v", err)
}

// Return the response.
return &decoded, nil
{{else}}
// Return.
return nil
{{end}}
}
5 changes: 5 additions & 0 deletions cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,11 @@ func (data *Data) generateOneOfType(name string, s *openapi3.Schema, spec *opena
}, enumDocs)
}

if len(s.OneOf) == 1 && s.OneOf[0].Value.Type == "object" {
// We need to generate the one of type.
return data.generateSchemaType(name, s.OneOf[0].Value, spec)
}

// Check if they all have a type.
types := []string{}
typeName := ""
Expand Down
Loading

0 comments on commit c71ff96

Please sign in to comment.