Skip to content

Commit a67c428

Browse files
authored
Merge pull request #1752 from BetterAndBetterII/fix/const-uri-uncompilable
fix(gen): encode const URI/string formats with e.Str
2 parents c72b101 + 168d98a commit a67c428

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

‎gen/_template/json/encoders_struct.tmpl‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
{{- $j := $g.JSON }}
2323
{{- if $j.TimeFormat }}
2424
json.EncodeTimeFormat(e, {{ $constVal }}, {{ $j.TimeFormat }})
25+
{{- else if and $j.Encoder (not $j.Fn) (eq $j.Type "String") }}
26+
{{- /* Const is a JSON string; EncodeURI/UUID/Date expect typed Go values. */}}
27+
e.Str({{ $constVal }})
2528
{{- else if $j.Encoder }}
2629
json.Encode{{ $j.Encoder }}(e, {{ $constVal }})
2730
{{- else if $j.Fn }}
@@ -37,6 +40,9 @@
3740
{{- $j := $t.JSON }}
3841
{{- if $j.TimeFormat }}
3942
json.EncodeTimeFormat(e, {{ $constVal }}, {{ $j.TimeFormat }})
43+
{{- else if and $j.Encoder (not $j.Fn) (eq $j.Type "String") }}
44+
{{- /* Const is a JSON string; EncodeURI/UUID/Date expect typed Go values. */}}
45+
e.Str({{ $constVal }})
4046
{{- else if $j.Encoder }}
4147
json.Encode{{ $j.Encoder }}(e, {{ $constVal }})
4248
{{- else if $j.Fn }}

‎gen/const_uri_encode_test.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package gen
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/ogen-go/ogen/jsonschema"
9+
)
10+
11+
type captureFS struct {
12+
files map[string][]byte
13+
}
14+
15+
func (c *captureFS) WriteFile(baseName string, source []byte) error {
16+
if c.files == nil {
17+
c.files = map[string][]byte{}
18+
}
19+
c.files[baseName] = source
20+
return nil
21+
}
22+
23+
func TestGenerateSchemaConstURICompiles(t *testing.T) {
24+
a := require.New(t)
25+
26+
const uri = "https://example.com/context.jsonld"
27+
schema := &jsonschema.Schema{
28+
Type: jsonschema.Object,
29+
Properties: []jsonschema.Property{
30+
{
31+
Name: "@context",
32+
Schema: &jsonschema.Schema{
33+
Type: jsonschema.String,
34+
Format: "uri",
35+
Const: uri,
36+
ConstSet: true,
37+
},
38+
Required: true,
39+
},
40+
{
41+
Name: "homepage",
42+
Schema: &jsonschema.Schema{
43+
Type: jsonschema.String,
44+
Format: "uri",
45+
Const: "https://example.com/",
46+
ConstSet: true,
47+
},
48+
},
49+
},
50+
}
51+
52+
fs := &captureFS{}
53+
err := GenerateSchema(schema, fs, GenerateSchemaOptions{
54+
TypeName: "Source",
55+
FileName: "output.gen.go",
56+
PkgName: "output",
57+
})
58+
a.NoError(err)
59+
60+
src, ok := fs.files["output.gen.go"]
61+
a.True(ok, "expected generated output.gen.go")
62+
got := string(src)
63+
64+
// Regression for ogen-go/ogen#1751: const uri was passed as a string to
65+
// json.EncodeURI, which expects url.URL, so generated code did not compile.
66+
a.NotContains(got, `json.EncodeURI(e, "`+uri+`")`)
67+
a.NotContains(got, `json.EncodeURI(e, "https://example.com/")`)
68+
a.Contains(got, `e.Str("`+uri+`")`)
69+
a.Contains(got, `e.Str("https://example.com/")`)
70+
}

0 commit comments

Comments
 (0)