Content-Length: 518284 | pFad | http://github.com/oapi-codegen/oapi-codegen/commit/48dc3d258cecc27c85448dd3d4bbd313e061f423

09 feat: add support for x-omitzero · oapi-codegen/oapi-codegen@48dc3d2 · GitHub
Skip to content

Commit 48dc3d2

Browse files
committed
feat: add support for x-omitzero
1 parent c8cf342 commit 48dc3d2

File tree

6 files changed

+88
-19
lines changed

6 files changed

+88
-19
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: x-omitempty
5+
components:
6+
schemas:
7+
Client:
8+
type: object
9+
required:
10+
- name
11+
properties:
12+
name:
13+
type: string
14+
id:
15+
type: number
16+
ClientWithExtension:
17+
type: object
18+
required:
19+
- name
20+
properties:
21+
name:
22+
type: string
23+
x-omitzero: true
24+
id:
25+
type: number
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# yaml-language-server: $schema=../../../configuration-schema.json
2+
package: xomitzero
3+
output: gen.go
4+
generate:
5+
models: true
6+
output-options:
7+
# to make sure that all types are generated, even if they're unreferenced
8+
skip-prune: true

examples/extensions/xomitzero/gen.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package xomitzero
2+
3+
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen -config cfg.yaml api.yaml

pkg/codegen/extension.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
extGoTypeName = "x-go-type-name"
1919
extPropGoJsonIgnore = "x-go-json-ignore"
2020
extPropOmitEmpty = "x-omitempty"
21+
extPropOmitZero = "x-omitzero"
2122
extPropExtraTags = "x-oapi-codegen-extra-tags"
2223
extEnumVarNames = "x-enum-varnames"
2324
extEnumNames = "x-enumNames"
@@ -60,6 +61,14 @@ func extParseOmitEmpty(extPropValue interface{}) (bool, error) {
6061
return omitEmpty, nil
6162
}
6263

64+
func extParseOmitZero(extPropValue interface{}) (bool, error) {
65+
omitZero, ok := extPropValue.(bool)
66+
if !ok {
67+
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
68+
}
69+
return omitZero, nil
70+
}
71+
6372
func extExtraTags(extPropValue interface{}) (map[string]string, error) {
6473
tagsI, ok := extPropValue.(map[string]interface{})
6574
if !ok {

pkg/codegen/schema.go

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,13 @@ type FieldDescriptor struct {
674674
IsRef bool // Is this schema a reference to predefined object?
675675
}
676676

677+
func stringOrEmpty(b bool, s string) string {
678+
if b {
679+
return s
680+
}
681+
return ""
682+
}
683+
677684
// GenFieldsFromProperties produce corresponding field names with JSON annotations,
678685
// given a list of schema descriptors
679686
func GenFieldsFromProperties(props []Property) []string {
@@ -724,31 +731,32 @@ func GenFieldsFromProperties(props []Property) []string {
724731
omitEmpty = shouldOmitEmpty
725732
}
726733

727-
// Support x-omitempty
734+
omitZero := false
735+
736+
// Support x-omitempty and x-omitzero
728737
if extOmitEmptyValue, ok := p.Extensions[extPropOmitEmpty]; ok {
729-
if extOmitEmpty, err := extParseOmitEmpty(extOmitEmptyValue); err == nil {
730-
omitEmpty = extOmitEmpty
738+
if xValue, err := extParseOmitEmpty(extOmitEmptyValue); err == nil {
739+
omitEmpty = xValue
740+
}
741+
}
742+
743+
if extOmitEmptyValue, ok := p.Extensions[extPropOmitZero]; ok {
744+
if xValue, err := extParseOmitZero(extOmitEmptyValue); err == nil {
745+
omitZero = xValue
731746
}
732747
}
733748

734749
fieldTags := make(map[string]string)
735750

736-
if !omitEmpty {
737-
fieldTags["json"] = p.JsonFieldName
738-
if globalState.options.OutputOptions.EnableYamlTags {
739-
fieldTags["yaml"] = p.JsonFieldName
740-
}
741-
if p.NeedsFormTag {
742-
fieldTags["form"] = p.JsonFieldName
743-
}
744-
} else {
745-
fieldTags["json"] = p.JsonFieldName + ",omitempty"
746-
if globalState.options.OutputOptions.EnableYamlTags {
747-
fieldTags["yaml"] = p.JsonFieldName + ",omitempty"
748-
}
749-
if p.NeedsFormTag {
750-
fieldTags["form"] = p.JsonFieldName + ",omitempty"
751-
}
751+
fieldTags["json"] = p.JsonFieldName +
752+
stringOrEmpty(omitEmpty, ",omitempty") +
753+
stringOrEmpty(omitZero, ",omitzero")
754+
755+
if globalState.options.OutputOptions.EnableYamlTags {
756+
fieldTags["yaml"] = p.JsonFieldName + stringOrEmpty(omitEmpty, ",omitempty")
757+
}
758+
if p.NeedsFormTag {
759+
fieldTags["form"] = p.JsonFieldName + stringOrEmpty(omitEmpty, ",omitempty")
752760
}
753761

754762
// Support x-go-json-ignore

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/oapi-codegen/oapi-codegen/commit/48dc3d258cecc27c85448dd3d4bbd313e061f423

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy