Skip to content

Commit

Permalink
Merge pull request #1312 from tdakkota/fix/issue-1310
Browse files Browse the repository at this point in the history
fix(gen): initialize pointer generics
  • Loading branch information
tdakkota authored Sep 10, 2024
2 parents 8e1b908 + 34afe31 commit 61c2669
Show file tree
Hide file tree
Showing 37 changed files with 2,369 additions and 70 deletions.
38 changes: 38 additions & 0 deletions _testdata/positive/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,23 @@
}
}
}
},
"/testIssue1310": {
"get": {
"operationId": "testIssue1310",
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Issue1310"
}
}
}
}
}
}
}
},
"components": {
Expand Down Expand Up @@ -1973,6 +1990,27 @@
"type": "string"
}
}
},
"Issue1310": {
"description": "An API error",
"type": "object",
"properties": {
"title": {
"description": "A short, human-readable summary of the problem type. This value should not change between occurrences of the error.",
"example": "Bad Request",
"type": "string"
},
"details": {
"description": "A human-readable explanation specific to this occurrence of the problem.",
"example": "Property foo is required but is missing.",
"type": "string"
},
"properties": {
"description": "Optional map of properties",
"nullable": true,
"type": "object"
}
}
}
},
"parameters": {
Expand Down
4 changes: 4 additions & 0 deletions examples/ex_openai/oas_json_gen.go

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

7 changes: 6 additions & 1 deletion gen/_template/json/encoders_generic.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ func (o *{{ $.Name }}) Decode(d *jx.Decoder{{ if $g.Format }}, format func(*jx.D
return err
}
o.Value = v
{{- else if or ($g.IsStruct) ($g.IsEnum) ($g.IsPointer) ($g.IsSum) ($g.IsAlias) }}
{{- else if $g.IsPointer }}
o.Value = new({{ $g.PointerTo.Go }})
if err := o.Value.Decode(d); err != nil {
return err
}
{{- else if or ($g.IsStruct) ($g.IsEnum) ($g.IsSum) ($g.IsAlias) }}
if err := o.Value.Decode(d); err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions internal/integration/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ func TestJSONGenerics(t *testing.T) {
})
}
})
t.Run("Issue1310", func(t *testing.T) {
t.Parallel()

val := api.Issue1310{
Title: api.NewOptString("Bad Request"),
Details: api.NewOptString("This is an example error"),
Properties: api.NewOptIssue1310Properties(&api.Issue1310Properties{}),
}
encoded := encodeObject(&val)

var decoded api.Issue1310
decodeObject(t, encoded, &decoded)
require.Equal(t, val, decoded)
require.JSONEq(t, string(encoded), string(encodeObject(&decoded)))
})
}

func TestJSONArray(t *testing.T) {
Expand Down
74 changes: 74 additions & 0 deletions internal/integration/sample_api/oas_client_gen.go

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

32 changes: 32 additions & 0 deletions internal/integration/sample_api/oas_faker_gen.go

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

91 changes: 91 additions & 0 deletions internal/integration/sample_api/oas_handlers_gen.go

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

Loading

0 comments on commit 61c2669

Please sign in to comment.