Skip to content

Commit

Permalink
feat: add query tag
Browse files Browse the repository at this point in the history
  • Loading branch information
jun.yoshida committed Feb 27, 2024
1 parent 1da4d42 commit 9818d7b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pkg/codegen/codegen_test.go
Expand Up @@ -82,7 +82,7 @@ type GetTestByNameResponse struct {

// Check the client method signatures:
assert.Contains(t, code, "type GetTestByNameParams struct {")
assert.Contains(t, code, "Top *int `form:\"$top,omitempty\" json:\"$top,omitempty\"`")
assert.Contains(t, code, "Top *int `form:\"$top,omitempty\" json:\"$top,omitempty\" query:\"$top,omitempty\"`")
assert.Contains(t, code, "func (c *Client) GetTestByName(ctx context.Context, name string, params *GetTestByNameParams, reqEditors ...RequestEditorFn) (*http.Response, error) {")
assert.Contains(t, code, "func (c *ClientWithResponses) GetTestByNameWithResponse(ctx context.Context, name string, params *GetTestByNameParams, reqEditors ...RequestEditorFn) (*GetTestByNameResponse, error) {")
assert.Contains(t, code, "DeadSince *time.Time `json:\"dead_since,omitempty\" tag1:\"value1\" tag2:\"value2\"`")
Expand Down
7 changes: 5 additions & 2 deletions pkg/codegen/operations.go
Expand Up @@ -90,7 +90,9 @@ func (pd *ParameterDefinition) Style() string {
switch in {
case "path", "header":
return "simple"
case "query", "cookie":
case "query":
return "query"
case "cookie":
return "form"
default:
panic("unknown parameter format")
Expand Down Expand Up @@ -912,7 +914,8 @@ func GenerateParamsTypes(op OperationDefinition) []TypeDefinition {
JsonFieldName: param.ParamName,
Required: param.Required,
Schema: pSchema,
NeedsFormTag: param.Style() == "form",
NeedsFormTag: param.Style() == "form" || param.Style() == "query",
NeedsQueryTag: param.Style() == "query",
Extensions: param.Spec.Extensions,
}
s.Properties = append(s.Properties, prop)
Expand Down
7 changes: 7 additions & 0 deletions pkg/codegen/schema.go
Expand Up @@ -93,6 +93,7 @@ type Property struct {
ReadOnly bool
WriteOnly bool
NeedsFormTag bool
NeedsQueryTag bool
Extensions map[string]interface{}
Deprecated bool
}
Expand Down Expand Up @@ -722,11 +723,17 @@ func GenFieldsFromProperties(props []Property) []string {
if p.NeedsFormTag {
fieldTags["form"] = p.JsonFieldName
}
if p.NeedsQueryTag {
fieldTags["query"] = p.JsonFieldName
}
} else {
fieldTags["json"] = p.JsonFieldName + ",omitempty"
if p.NeedsFormTag {
fieldTags["form"] = p.JsonFieldName + ",omitempty"
}
if p.NeedsQueryTag {
fieldTags["query"] = p.JsonFieldName + ",omitempty"
}
}

// Support x-go-json-ignore
Expand Down

0 comments on commit 9818d7b

Please sign in to comment.