oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

deepObject with additionalProperties returns unhandled type: map[string]string error

Open goblindegook opened this issue 2 years ago • 1 comments

Hi, I'm getting an error handling requests for the following definition:

openapi: 3.0.0
info:
  title: Example
paths:
  /products:
    get:
      operationId: listProducts
      parameters:
        - name: filter
          in: query
          style: deepObject
          explode: true
          schema:
            type: object
            additionalProperties:
              type: string
      responses:
        '200':
          content:
            application/json:
              schema:
                type: object

The generated type is *map[string]string as expected but handling the request throws an error: unhandled type: map[string]string.

I can see using the debugger that the query string is successfully parsed and fieldPaths seems correct, but it fails in assignPathValues when attempting to write the values to a map, which isn't supported as there is no case for reflect.Map.

This ought to work, and there is a unit test for BindQueryParameter in the codebase that covers a similar scenario, so I'm stumped. What am I missing here?

Thanks in advance!

goblindegook avatar Jan 20 '23 18:01 goblindegook

Tried this in the hopes that a struct would work around the problem:

- name: filter
          in: query
          style: deepObject
          explode: true
          schema:
            type: object
            properties:
                 color:
                     type: string
                 size:
                     type: string
            additionalProperties:
              type: string

This generates the following type, which (again) makes sense:

type ListProductsParams_Filter struct {
	Color    *string           `json:"color,omitempty"`
	Size     *string           `json:"size,omitempty"`
	AdditionalProperties map[string]string `json:"-"`
}

However, making a request with /list_products?filter[color]=blue&filter[name]=foo fails with a 400 response. Using debug I see that the following error is thrown now:

error assigning value to destination: field [name] is not present in destination object

What is the correct way to handle arbitrary param names in an query string object?

goblindegook avatar Jan 23 '23 09:01 goblindegook

@goblindegook , did you find a solution to this usecase?

gautamborad avatar Apr 01 '23 09:04 gautamborad

@gautamborad There is an open pull request here: #934. I have to use a fork of this project otherwise I can't get this particular schema definition to work.

goblindegook avatar Apr 02 '23 18:04 goblindegook