Skip to content

kin-openapi: Unable to Unmarshal OpenAPI 2.0 spec with nonBodyParameters #1013

@PellePedro

Description

@PellePedro

I'm encountering an issue when parsing OpenAPI 2.0 specifications with non-body parameters (e.g., path, query, header parameters) using the kin-openapi library.

Unmarshalling a OpenAPI 2.0 spec to an openapi2.T instance fails for non-body parameters like the ones in this spec: https://raw.githubusercontent.com/tcorman/pubstest/master/cpqapiv2.yaml.

Expected Behavior

According to the OpenAPI 2.0 specification, parameters are defined as either bodyParameter or nonBodyParameter. Both should be handled during unmarshalling. Non-body parameters, such as those located in the path or query string, should be parsed correctly into types like string, number, or array.

However it seems that the kin-openapi library currently only implements parsing for bodyParameter types. The openapi2.Parameter struct uses the Type *openapi3.Types field, which doesn’t account for the type field in non-body parameters.

Actual Behavior

The unmarshalling process fails when non-body parameters are encountered. This is likely because the library expects a schema field in all parameters (as required by body parameters) but fails to account for non-body parameters that define their type with the type field instead.

Here’s an excerpt from the OpenAPI 2.0 schema:

"parameter": {
  "oneOf": [
    { "$ref": "#/definitions/bodyParameter" },
    { "$ref": "#/definitions/nonBodyParameter" }
  ]
},
"nonBodyParameter": {
  "type": "object",
  "required": ["name", "in", "type"],
  "oneOf": [
    { "$ref": "#/definitions/headerParameterSubSchema" },
    { "$ref": "#/definitions/formDataParameterSubSchema" },
    { "$ref": "#/definitions/queryParameterSubSchema" },
    { "$ref": "#/definitions/pathParameterSubSchema" }
  ]
},
"pathParameterSubSchema": {
  "type": "object",
  "properties": {
    "in": { "type": "string", "enum": ["path"] },
    "type": { "type": "string", "enum": ["string", "number", "boolean", "integer", "array"] },
    "required": { "type": "boolean", "enum": [true] }
  }
}

Currently, the kin-openapi library only seems to handle the bodyParameter case, which includes a schema field. The non-body parameters rely on fields like type, in, and required, but the library attempts to parse them as body parameters, causing an error.

Steps to Reproduce

Here’s a simple test case to reproduce this issue:

package main

import (
	"io"
	"net/http"
	"testing"
	"github.com/getkin/kin-openapi/openapi2"
	"gopkg.in/yaml.v3"
)

func TestSwagger2OpenApi3(t *testing.T) {
	restInputUrl := "https://raw.githubusercontent.com/tcorman/pubstest/master/cpqapiv2.yaml"
	resp, err := http.Get(restInputUrl)
	if err != nil {
		t.Fatal(err)
	}
	defer resp.Body.Close()

	input, err := io.ReadAll(resp.Body)
	if err != nil {
		t.Fatal(err)
	}

	var doc openapi2.T
	if err = yaml.Unmarshal(input, &doc); err != nil {
		t.Fatal(err)
	}
}

Error Message

The following error occurs when attempting to unmarshal non-body parameters in the spec:

[]string len: 76, cap: 143, [
  "line 24: cannot unmarshal !!str `string` into openapi3.Types",
  "line 31: cannot unmarshal !!str `string` into openapi3.Types",
  "line 38: cannot unmarshal !!str `string` into openapi3.Types",
  "line 45: cannot unmarshal !!str `array` into openapi3.Types"
]

Relevant Links

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions