Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions configuration-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@
"required": [
"path"
]
},
"yaml-tags": {
"type": "boolean",
"description": "Enable the generation of YAML tags for struct fields"
}
}
},
Expand Down
8 changes: 8 additions & 0 deletions internal/test/outputoptions/yaml-tags/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# yaml-language-server: $schema=../../../../configuration-schema.json
package: yamltags
generate:
models: true
output-options:
skip-prune: true
yaml-tags: true
output: yamltags.go
3 changes: 3 additions & 0 deletions internal/test/outputoptions/yaml-tags/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package yamltags

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=config.yaml spec.yaml
24 changes: 24 additions & 0 deletions internal/test/outputoptions/yaml-tags/spec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
openapi: "3.0.1"
info:
version: 1.0.0
title: Cookie parameters
paths:
/cookies:
get:
operationId: cookieParams
parameters:
- name: authId
description: Cookie parameter
in: cookie
required: false
schema:
type: string
- name: serverId
description: Another cookie parameter
in: cookie
required: false
schema:
type: string
responses:
204:
description: no content
13 changes: 13 additions & 0 deletions internal/test/outputoptions/yaml-tags/yamltags.go

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

3 changes: 3 additions & 0 deletions pkg/codegen/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ type OutputOptions struct {

// Overlay defines configuration for the OpenAPI Overlay (https://github.com/OAI/Overlay-Specification) to manipulate the OpenAPI specification before generation. This allows modifying the specification without needing to apply changes directly to it, making it easier to keep it up-to-date.
Overlay OutputOptionsOverlay `yaml:"overlay"`

// EnableYamlTags adds YAML tags to generated structs, in addition to default JSON ones
EnableYamlTags bool `yaml:"yaml-tags,omitempty"`
}

func (oo OutputOptions) Validate() map[string]string {
Expand Down
6 changes: 6 additions & 0 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,11 +728,17 @@ func GenFieldsFromProperties(props []Property) []string {

if !omitEmpty {
fieldTags["json"] = p.JsonFieldName
if globalState.options.OutputOptions.EnableYamlTags {
fieldTags["yaml"] = p.JsonFieldName
}
if p.NeedsFormTag {
fieldTags["form"] = p.JsonFieldName
}
} else {
fieldTags["json"] = p.JsonFieldName + ",omitempty"
if globalState.options.OutputOptions.EnableYamlTags {
fieldTags["yaml"] = p.JsonFieldName + ",omitempty"
}
if p.NeedsFormTag {
fieldTags["form"] = p.JsonFieldName + ",omitempty"
}
Expand Down