Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
PellePedro opened this issue Sep 26, 2024 · 1 comment
Open

Comments

@PellePedro
Copy link

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

@isaswa
Copy link

isaswa commented Nov 7, 2024

I encountered the same issue on those non-body parameters under a path cannot be unmarshaled with a 2.0 spec.
There seem to be no other packages that could convert v2 to v3 spec, so this issue here is quite crucial IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants