Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate server: UnmarshalJSON redeclared for object with default values when --strict-additional-properties specified #2189

Open
mdelvecchio-wasabi opened this issue Jan 22, 2020 · 1 comment
Assignees
Labels
bug defaults model Related to swagger generate model command

Comments

@mdelvecchio-wasabi
Copy link

mdelvecchio-wasabi commented Jan 22, 2020

Problem statement

Using go-swagger to generate a server. My schema defines an object with default values specified for some properties, and I am specifying the --strict-additional-properties command-line option.

The generated code has two instances of the UnmarshalJSON() method for this object, resulting in a compilation error.

If the server is generated without the --strict-additional-properties command-line option, only one UnmarshalJSON() method is generated.

In addition, if the default values are removed from the Swagger definition, only one UnmarshalJSON() method is generated.

Please remove the sections that don't apply

Swagger specification

swagger: '2.0'
info:
  title: Test API
  description: Test
  version: v1
host: host
schemes:
- https
- http
basePath: /path
produces:
- application/json

paths:
  /obj:
    get:
      summary: Get an object
      operationId: getObject
      responses:
        200:
          description: The object
          schema:
            $ref: '#/definitions/MyObject'

definitions:
  MyObject:
    title: My object
    type: object
    properties:
      string1:
        type: string
        title: A string
      bool1:
        type: boolean
        title: A boolean
    default:
      string1: "first"
      bool1:   false

Steps to reproduce

  1. Generate the server:
./go-swagger generate server --strict-additional-properties
  1. Inspect the resulting models/my_object.go file, and see two definitions for UnmarshalJSON():
func (m *MyObject) UnmarshalJSON(b []byte) error {
	type MyObjectAlias MyObject
	var t MyObjectAlias
	if err := json.Unmarshal([]byte("{\"bool1\":false,\"string1\":\"first\"}"), &t); err != nil {
		return err
	}
	if err := json.Unmarshal(b, &t); err != nil {
		return err
	}
	*m = MyObject(t)
	return nil
}

// UnmarshalJSON unmarshals this object while disallowing additional properties from JSON
func (m *MyObject) UnmarshalJSON(data []byte) error {
	var props struct {

		// A boolean
		Bool1 bool `json:"bool1,omitempty"`

		// A string
		String1 string `json:"string1,omitempty"`
	}

	dec := json.NewDecoder(bytes.NewReader(data))
	dec.DisallowUnknownFields()
	if err := dec.Decode(&props); err != nil {
		return err
	}

	m.Bool1 = props.Bool1

	m.String1 = props.String1

	return nil
}


Environment

swagger version: v0.21.0
go version: go1.11.5
OS: Linux

@fredbi fredbi added bug model Related to swagger generate model command labels Jan 26, 2020
@fredbi fredbi self-assigned this Jan 29, 2020
@fredbi
Copy link
Contributor

fredbi commented Jan 29, 2020

Thanks for reporting this. The problem is indeed more general than this specific use case: unmarshallers with default values conflict with other unmarshallers. I am reshuffling the templates for serializers so we can get a clearer view on this.

fredbi added a commit to fredbi/go-swagger that referenced this issue Jan 29, 2020
…LITY

* reshuffled serializers templates in a separate directory, split the templates by schema type
* reindented model templates, using {{- }}, fixed some issues with template blank gobbling
* revamped docstring (fixed unattended line feed, removed unused hardcoded reference)
* regenerated examples

* contributes to forthcoming fix for go-swagger#2189

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Jan 29, 2020
…LITY

* reshuffled serializers templates in a separate directory, split the templates by schema type
* reindented model templates, using {{- }}, fixed some issues with template blank gobbling
* revamped docstring (fixed unattended line feed after comments, removed unused hardcoded reference)
* regenerated examples

* contributes to forthcoming fix for go-swagger#2189

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/go-swagger that referenced this issue Feb 8, 2020
…LITY

Motivation: I wanted to fix some bugs with model generation, but needed to clarify things a bit beforehand

* reshuffled serializers templates in a separate directory, split the templates by schema type
* reindented model templates, using {{- }}, fixed some issues with template blank gobbling
* revamped docstring (fixed unattended line feed after comments, removed unused hardcoded reference)

* fixes go-swagger#2215 (syntax error in templates, no more tolerated by go1.14)
* contributes to forthcoming fixes for go-swagger#2189 (serializers with defaults), go-swagger#2220 (edge cases on polymorphic types)
* contributes to go-swagger#673 (templates readability)

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

Adapted test expectations to slight modifications of how goformat ends up when some blank lines are removed

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

Regenerated examples

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
@fredbi fredbi added the defaults label Feb 8, 2020
casualjim pushed a commit that referenced this issue Feb 8, 2020
…LITY

Motivation: I wanted to fix some bugs with model generation, but needed to clarify things a bit beforehand

* reshuffled serializers templates in a separate directory, split the templates by schema type
* reindented model templates, using {{- }}, fixed some issues with template blank gobbling
* revamped docstring (fixed unattended line feed after comments, removed unused hardcoded reference)

* fixes #2215 (syntax error in templates, no more tolerated by go1.14)
* contributes to forthcoming fixes for #2189 (serializers with defaults), #2220 (edge cases on polymorphic types)
* contributes to #673 (templates readability)

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

Adapted test expectations to slight modifications of how goformat ends up when some blank lines are removed

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>

Regenerated examples

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug defaults model Related to swagger generate model command
Projects
None yet
Development

No branches or pull requests

2 participants