Skip to content

Validation code for maxProperties is generated incorrectly #2587

@veleek

Description

@veleek

Problem statement

When using the maxProperties property in a schema, then generated validation logic incorrectly checks the length of the base object as opposed to the length of the property that's being validated.

func (m *SomeThing) validateData(formats strfmt.Registry) error {
	if swag.IsZero(m.Data) { // not required
		return nil
	}

	nprops := len(m) // <-- ERROR: len(m) should be len(m.Data)

	// maxProperties: 20
	if nprops > 20 {
		return errors.TooManyProperties("data", "body", 20)
	}

	return nil
}

Swagger specification

    "SomeThing": {
      "properties": {
        "data": {
          "additionalProperties": {
            "type": "object"
          },
          "maxProperties": 20,
          "type": "object",
          "x-nullable": true
        }
      },
      "type": "object"
    },

Steps to reproduce

  1. Create a schema with a component schema that has the above content
  2. Generate models swagger generate models -f ./spec.json -t ./out
  3. Generated code is non-compilable.

Environment

swagger version: v0.26.1
go version: go1.16.4 darwin/amd64
OS: MacOS

Metadata

Metadata

Assignees

Labels

bugmodelRelated to swagger generate model commandpending PRvalidatorRelated to codegen generation of validations

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions