Skip to content

Commit

Permalink
Disable omitempty
Browse files Browse the repository at this point in the history
  • Loading branch information
ptodev committed Sep 19, 2024
1 parent a74cd4e commit 57bc4f1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ type Config struct {
OnlyModels bool
MinSizedInts bool
Loader schemas.Loader
// When DisableOmitempty is set to true,
// an "omitempty" tag will never be present in generated struct fields.
// When DisableOmitempty is set to false,
// an "omitempty" tag will be present for ann fields that are not required.
DisableOmitempty bool
}

type SchemaMapping struct {
Expand Down
4 changes: 4 additions & 0 deletions pkg/generator/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,7 @@ func (g *Generator) makeEnumConstantName(typeName, value string) string {

return typeName + g.caser.Identifierize(value)
}

func (g *Generator) DisableOmitempty() bool {
return g.config.DisableOmitempty
}
2 changes: 1 addition & 1 deletion pkg/generator/schema_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func (g *schemaGenerator) generateStructType(

tags := ""

if isRequired {
if isRequired || g.DisableOmitempty() {
for _, tag := range g.config.Tags {
tags += fmt.Sprintf(`%s:"%s" `, tag, name)
}
Expand Down
8 changes: 8 additions & 0 deletions tests/data/disableOmitempty/omitempty.go

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

10 changes: 10 additions & 0 deletions tests/data/disableOmitempty/omitempty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"id": "https://example.com/imported",
"type": "object",
"properties": {
"importedString": {
"type": "string"
}
}
}
13 changes: 12 additions & 1 deletion tests/generation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ func TestCore(t *testing.T) {
testExamples(t, basicConfig, "./data/core")
}

func TestOmitempty(t *testing.T) {
t.Parallel()

cfg := basicConfig
cfg.DisableOmitempty = true

testExamples(t, cfg, "./data/disableOmitempty")
}

func TestValidation(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -243,7 +252,9 @@ func testExampleFile(t *testing.T, cfg generator.Config, fileName string) {
}

if diff, ok := diffStrings(t, string(goldenData), string(source)); !ok {
t.Fatalf("Contents different (left is expected, right is actual):\n%s", *diff)
// TODO: Revert this later
// t.Fatalf("Contents different (left is expected, right is actual):\n%s", *diff)
t.Fatalf("Contents different. Actual:\n%s\nDiff:\n%s", string(source), *diff)
}
}
})
Expand Down

0 comments on commit 57bc4f1

Please sign in to comment.