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

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" #1016

Open
jlsherrill opened this issue Oct 7, 2024 · 1 comment

Comments

@jlsherrill
Copy link
Contributor

jlsherrill commented Oct 7, 2024

This was introduced by #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
Copy link
Contributor Author

jlsherrill commented Oct 7, 2024

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

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

1 participant