Skip to content

go-swagger is generating invalid validation functions #2163

@jeffreydwalter

Description

@jeffreydwalter

The validation functions generated for number types like the following:

        "AmountToAllocate": {
          "format": "double",
          "maximum": 1000000000,
          "minimum": 1,
          "pattern": "[-]?\\d+(.\\d{1,2})?",
          "type": "number"
        },

generate types and validation functions like so:

	// amount to allocate
	// Required: true
	// Maximum: 1e+09
	// Minimum: 1
	// Pattern: [-]?\d+(.\d{1,2})?
	AmountToAllocate *float64 `json:"AmountToAllocate"`

and

func (m *AllocationPlan) validateAmountToAllocate(formats strfmt.Registry) error {
	if err := validate.Required("AmountToAllocate", "body", m.AmountToAllocate); err != nil {
		return err
	}
	if err := validate.Pattern("AmountToAllocate", "body", string(*m.AmountToAllocate), `[-]?\d+(.\d{1,2})?`); err != nil {
		return err
	}
	if err := validate.Minimum("AmountToAllocate", "body", float64(*m.AmountToAllocate), 1, false); err != nil {
		return err
	}
	if err := validate.Maximum("AmountToAllocate", "body", float64(*m.AmountToAllocate), 1e+09, false); err != nil {
		return err
	}
	return nil
}

This leads to compiler errors like:

/models/allocation_plan.go:132:63: cannot convert *m.AmountToAllocate (type float64) to type string
/models/allocation_plan.go:183:65: cannot convert m.QuantityToAllocate (type float64) to type string
/models/allocation_plan.go:222:63: cannot convert m.WeightToAllocate (type float64) to type string
...
/models/company_rot_rut_settings_api.go:150:76: too many errors

The problem is that go-swagger is generating code that's casting a *float64 to a string: e.g. string(*m.AmountToAllocate).

Steps to reproduce

Generated swagger client like so: swagger generate client -f swagger.yml

Environment

swagger version: v0.21.0
commit: 7c0fc3ee340f9d99d85573bc54a57e303a639692
go version: go1.13.5 linux/amd64
OS:  Linux 5.0.0-37-generic #40~18.04.1-Ubuntu SMP Thu Nov 14 12:06:39 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

Metadata

Metadata

Assignees

Labels

enhancementmodelRelated to swagger generate model commandpending PRvalidate specRelated to github.com/go-openapi/validate

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions