kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

arrays of objects as query parameters cause a crush

Open orensolo opened this issue 3 years ago • 0 comments

Hi,

I am using the latest kin-openapi (v0.103.0) This issue is similar to https://github.com/getkin/kin-openapi/issues/619. Query parameters that are defined as arrays of objects (i.e not primitives) causing kin-openapi to crush with panic (schema has non primitive type ...)

I prepared a code that you can run in order to reproduce the bug, please try it out to verify the bug:

package main

import (
	"context"
	"fmt"
	"net/http"

	"github.com/getkin/kin-openapi/openapi3"
	"github.com/getkin/kin-openapi/openapi3filter"
	"github.com/getkin/kin-openapi/routers/gorillamux"
)

func main() {
	ctx := context.Background()
	loader := &openapi3.Loader{Context: ctx, IsExternalRefsAllowed: true}
	schema :=
		`
openapi: 3.0.0
info:
 version: 1.0.0
 title: Sample API
paths:
 /items:
  get:
   parameters:
    - description: "test object"
      explode: false
      in: query
      name: test
      required: false
      schema:
       items:
        properties:
         name:
          type: string
        type: object
       type: array
   description: Returns a list of stuff              
   responses:
    '200':
     description: Successful response`

	doc, err := loader.LoadFromData([]byte(schema))

	if err != nil {
		fmt.Println("failed to load schema. " + err.Error())
		return
	}
	// Validate document

	if err = doc.Validate(ctx); err != nil {
		fmt.Println("failed to validate schema. " + err.Error())
		return
	}

	router, _ := gorillamux.NewRouter(doc)
	httpReq, _ := http.NewRequest(http.MethodGet, "/items?test=3", nil)

	// Find route
	route, pathParams, _ := router.FindRoute(httpReq)

	// Validate request
	requestValidationInput := &openapi3filter.RequestValidationInput{
		Request:    httpReq,
		PathParams: pathParams,
		Route:      route,
	}
	err = openapi3filter.ValidateRequest(ctx, requestValidationInput)
	if err != nil {
		fmt.Println("failed to validate request. " + err.Error())
		return
	}

}

When this code is executed, the following panic happen:

panic: schema has non primitive type "object"

goroutine 1 [running]:
github.com/getkin/kin-openapi/openapi3filter.parsePrimitive({0x919bd9, 0x1}, 0xc00000ea80)
	/home/ubuntu/kintest/vendor/github.com/getkin/kin-openapi/openapi3filter/req_resp_decoder.go:816 +0xc4e
github.com/getkin/kin-openapi/openapi3filter.parseArray({0xc00018f010, 0x1, 0x1}, 0xc00000e948)
	/home/ubuntu/kintest/vendor/github.com/getkin/kin-openapi/openapi3filter/req_resp_decoder.go:769 +0x1bc
github.com/getkin/kin-openapi/openapi3filter.(*urlValuesDecoder).DecodeArray(0xc000010690, {0xc000017ef0, 0x4}, 0xc00000ec78, 0xc00000e948)
	/home/ubuntu/kintest/vendor/github.com/getkin/kin-openapi/openapi3filter/req_resp_decoder.go:515 +0x725
github.com/getkin/kin-openapi/openapi3filter.decodeValue.func1({0xc000017ef0, 0x4}, 0xc00000ec78, 0xc00000e948)
	/home/ubuntu/kintest/vendor/github.com/getkin/kin-openapi/openapi3filter/req_resp_decoder.go:308 +0xb6
github.com/getkin/kin-openapi/openapi3filter.decodeValue({0x959ca8, 0xc000010690}, {0xc000017ef0, 0x4}, 0xc00000ec78, 0xc00000e948, 0x0)
	/home/ubuntu/kintest/vendor/github.com/getkin/kin-openapi/openapi3filter/req_resp_decoder.go:317 +0x562
github.com/getkin/kin-openapi/openapi3filter.decodeStyledParameter(0xc000150880, 0xc00019cc90)
	/home/ubuntu/kintest/vendor/github.com/getkin/kin-openapi/openapi3filter/req_resp_decoder.go:242 +0x62d
github.com/getkin/kin-openapi/openapi3filter.ValidateParameter({0x95b470, 0xc000016088}, 0xc00019cc90, 0xc000150880)
	/home/ubuntu/kintest/vendor/github.com/getkin/kin-openapi/openapi3filter/validate_request.go:140 +0x32c
github.com/getkin/kin-openapi/openapi3filter.ValidateRequest({0x95b470, 0xc000016088}, 0xc00019cc90)
	/home/ubuntu/kintest/vendor/github.com/getkin/kin-openapi/openapi3filter/validate_request.go:83 +0x5f3
main.main()
	/home/ubuntu/kintest/main1.go:68 +0x579

Thanks in advance,

Oren

orensolo avatar Oct 06 '22 15:10 orensolo