go-jsonschema icon indicating copy to clipboard operation
go-jsonschema copied to clipboard

Folded blocks in YAML cause extra line breaks

Open felixjung opened this issue 3 years ago • 1 comments

When generating code from a YAML schema definition that uses folded blocks for descriptions, the comments in the generated output code have a trailing comment line.

The issue may be related to go-yaml/yaml#789.

Example

Input

"$schema": "http://json-schema.org/draft-04/schema#"
"$id": "https://example.com/yaml_formatting"

title: MyObject
type: object
properties:
  foo:
    description: >
      I'm a multiline description in a folded block. Folded blocks should not
      have hard line breaks after parsing. They should also not end in a line break.
    type: string
  bar:
    description: |
      I'm a multiline description in a literal block. Literal blocks, on the other hand,
      should have hard line breaks and may end in a line break, too.

      This may look funky when Go code is generated to a specific line width, though.
    type: string

Expected Output

// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.

package test

type YamlMultilineDescriptionsYaml struct {
	// I'm a multiline description in a literal block. Literal blocks, on the other
	// hand,
	// should have hard line breaks and may end in a line break, too.
	//
	// This may look funky when Go code is generated to a specific line width, though.
	//
	Bar *string `json:"bar,omitempty" yaml:"bar,omitempty"`

	// I'm a multiline description in a folded block. Folded blocks should not have
	// hard line breaks after parsing. They should also not end in a line break.
	Foo *string `json:"foo,omitempty" yaml:"foo,omitempty"`
}

Actual Output

// Code generated by github.com/atombender/go-jsonschema, DO NOT EDIT.

package test

type YamlMultilineDescriptionsYaml struct {
	// I'm a multiline description in a literal block. Literal blocks, on the other
	// hand,
	// should have hard line breaks and may end in a line break, too.
	//
	// This may look funky when Go code is generated to a specific line width, though.
	//
	Bar *string `json:"bar,omitempty" yaml:"bar,omitempty"`

	// I'm a multiline description in a folded block. Folded blocks should not have
	// hard line breaks after parsing. They should also not end in a line break.
	//
	Foo *string `json:"foo,omitempty" yaml:"foo,omitempty"`
}

felixjung avatar Jan 18 '22 09:01 felixjung

I'm opening a PR to address this. The PR changes the yaml library, which I'm not sure you are happy to do.

felixjung avatar Jan 18 '22 09:01 felixjung

changes merged in #52

omissis avatar Apr 08 '23 21:04 omissis