Skip to content

oneOf combined with arrays results in unable to unmarshal json #1063

@Kellel

Description

@Kellel

The following schema:

openapi: "3.0.1"
info:
  version: 1.0.0
  title: Swagger Petstore
paths:
  /pet:
    get:
      description: |
        Returns pet from the system that the user has access to
      responses:
        '200':
          description: pet response
          content:
            application/json:
              schema:
                oneOf:
                 - $ref: '#/components/schemas/Pets'
                 - $ref: '#/components/schemas/Pets2'
components:
  schemas:
    Pets:
      type: array
      items:
        $ref: '#/components/schemas/Pet'
    Pets2:
      type: array
      items:
        $ref: '#/components/schemas/Pet2'
    Pet:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        tag:
          type: string
    Pet2:
      type: object
      required:
        - name
      properties:
        name:
          type: string
        tag:
          type: integer

Then a simple test:

func TestCanParsePet(t *testing.T) {
	test := `[{
	"name": "foo",
	"tag": "foo"
}]`

	r := ioutil.NopCloser(bytes.NewReader([]byte(test)))
	res := &http.Response{
		StatusCode: 200,
		Body:       r,
		Header:     make(http.Header),
	}
	res.Header.Set("content-type", "application/json")
	petResponse, err := ParseGetPetResponse(res)
	require.NoError(t, err)

	require.NotNil(t, petResponse.JSON200)
}

Results in :

--- FAIL: TestCanParsePet (0.00s)
    issue_test.go:26:
                Error Trace:    /home/test/test/issue_test.go:26
                Error:          Received unexpected error:
                                json: cannot unmarshal array into Go value of type struct { union json.RawMessage }
                Test:           TestCanParsePet
FAIL
exit status 1
FAIL    example.com/test        0.002s

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions