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

query parameter validation unmarshalling error

Open orensolo opened this issue 3 years ago • 0 comments

Hi,

I am using github.com/getkin/kin-openapi v0.103.0 (latest version).

If the parameter content is application/json, then kin-open api tries to unmarshel it (using json.unmarshal). But the parameter value need not be a json even if its content is application/json.

For example, when I run the following code :

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 non object"
      explode: true
      style: form
      in: query
      name: test
      required: false
      content:
       application/json:
         schema:
          anyOf:
           - type: string
           - type: integer
   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=test1`, 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
	}

}

I got the following error:

failed to validate request. parameter "test" in query has an error: error unmarshaling parameter "test"

But test=test1 is a valid parameter according to the schema. Please run the above code to reproduce the bug.

Thanks in advance, Oren

orensolo avatar Oct 06 '22 15:10 orensolo