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

Missing null check for array items #2382

Open
jdeflander opened this issue Aug 14, 2020 · 1 comment
Open

Missing null check for array items #2382

jdeflander opened this issue Aug 14, 2020 · 1 comment
Labels
bug model Related to swagger generate model command

Comments

@jdeflander
Copy link

Problem statement

go-swagger allows null values for array items where it shouldn't. When defining an array having items that result in Go pointers, go-swagger inserts a special case to skip validation for nil pointers.

Swagger specification

consumes:
  - application/json
info:
  title: Foo
  version: 1.0.0
paths:
  /foo:
    put:
      parameters:
        - in: body
          name: foo
          required: true
          schema:
            items:
              properties:
                foo:
                  type: boolean
              type: object
            type: array
      responses:
        200:
          description: OK
swagger: "2.0"

Steps to reproduce

  1. Generate a new server with the specification given above.
  2. Run the server.
  3. Perform a PUT request to the /foo endpoint with [null] as body and observe that validation succeeds.

Environment

swagger version: v0.25.0
go version: go1.13
OS: darwin

@fredbi fredbi added model Related to swagger generate model command bug labels Aug 16, 2020
@veleek
Copy link
Contributor

veleek commented Jun 29, 2021

Here's an example of the generated validation code that explicitly ignores nil values in an array.

func (m *QueryRequest) validateOrder(formats strfmt.Registry) error {
	if swag.IsZero(m.Order) { // not required
		return nil
	}

	for i := 0; i < len(m.Order); i++ {
		if swag.IsZero(m.Order[i]) { // not required
			continue
		}

		if m.Order[i] != nil {
			if err := m.Order[i].Validate(formats); err != nil {
				if ve, ok := err.(*errors.Validation); ok {
					return ve.ValidateName("order" + "." + strconv.Itoa(i))
				}
				return err
			}
		}

	}

	return nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug model Related to swagger generate model command
Projects
None yet
Development

No branches or pull requests

3 participants