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

Error on v2 to V3 conversion when using a hashmap/dict with $ref "failed to resolve "definitions" in fragment in URI: "#/definitions/": struct field "definitions" not found"

Open jlsherrill opened this issue 1 year ago • 1 comments

This was introduced by https://github.com/getkin/kin-openapi/pull/1011

within a v2 doc, anytime a map/dictionary is defined using a $ref, conversion to v3

fails:

		"PetDirectory":{
				"type": "object",
				"additionalProperties": {
					"$ref": "#/definitions/Pet"
				}
		}

Converting this results in:

failed to resolve "definitions" in fragment in URI: "#/definitions/Pet": struct field "definitions" not found

reproducer below

jlsherrill avatar Oct 07 '24 11:10 jlsherrill

Reproducer:

Simply drop this in openapi2conv/issues1016_test.go


import (
	"encoding/json"
	"testing"

	"github.com/getkin/kin-openapi/openapi2"
	"github.com/stretchr/testify/require"
)

func TestIssue1016(t *testing.T) {
	v2 := []byte(`
{
    "basePath": "/v2",
    "host": "test.example.com",
    "info": {
        "title": "MyAPI",
        "version": "0.1",
        "x-info": "info extension"
    },
    "paths": {
        "/foo": {
            "get": {
                "operationId": "getFoo",
                "responses": {
                    "200": {
                        "description": "returns all information",
                        "schema": {
                            "$ref": "#/definitions/PetDirectory"
                        }
                    },
                    "default": {
                        "description": "OK"
                    }
                },
                "summary": "get foo"
            }
        }
    },
    "schemes": [
        "http"
    ],
    "swagger": "2.0",
    "definitions": {
		"Pet": {
			"type": "object",
			"required": ["petType"],
			"properties": {
				"petType": {
					"type": "string"
				},
				"name": {
					"type": "string"
				},
				"age": {
					"type": "integer"
				}
			}
		},
		"PetDirectory":{
			"type": "object",
			"additionalProperties": {
				"$ref": "#/definitions/Pet"
			}
		}
    }
}
`)

	var doc2 openapi2.T
	err := json.Unmarshal(v2, &doc2)
	require.NoError(t, err)
	_, err = v2v3YAML(v2)
	require.NoError(t, err)
}
 go test ./...
?   	github.com/getkin/kin-openapi/cmd/validate	[no test files]
ok  	github.com/getkin/kin-openapi/openapi2	(cached)
?   	github.com/getkin/kin-openapi/openapi3gen/internal/subpkg	[no test files]
--- FAIL: TestIssue1016 (0.00s)
    issues1016_test.go:75: 
        	Error Trace:	/home/jlsherri/git/kin-openapi/openapi2conv/issues1016_test.go:75
        	Error:      	Received unexpected error:
        	            	failed to resolve "definitions" in fragment in URI: "#/definitions/Pet": struct field "definitions" not found
        	Test:       	TestIssue1016

jlsherrill avatar Oct 07 '24 11:10 jlsherrill