Skip to content

Getting Found unresolved ref errors during parse swagger 2.0 contract #187

@Dentrax

Description

@Dentrax

I just tried to test this project with our swagger 2.0 json file. During implementation, i couldn't find a life-saver v2 to v3 converter func or auto converter something like:

For 3.0: _ = openapi3filter.NewRouter().WithSwaggerV3FromFile("swagger.json")
For 2.0: _ = openapi3filter.NewRouter().WithSwaggerV2FromFile("swagger.json")

I found this simple implementation:

func V2ToV3(v2Data []byte) (*openapi3.Swagger, error) {
	v2Spec := &openapi2.Swagger{}
	if err := json.Unmarshal(v2Data, v2Spec); err != nil {
		return nil, err
	}
	v3Spec, err := openapi2conv.ToV3Swagger(v2Spec)
	if err != nil {
		return nil, err
	}
	return v3Spec, nil
}

Anyway, the main question is that actually why i am getting Found unresolved ref errors. openapi3filter couldn't recognize our $ref model i think. Simplified json is here:

{
  "swagger": "2.0",
  "info": {
    "description": "Test Golang Application",
    "version": "1.0",
    "title": "Test",
    "contact": {
      "name": "Test",
      "email": "test@test.com"
    }
  },
  "host": "",
  "basePath": "/test",
  "definitions": {
    "model.ProductSearchAttributeRequest": {
      "type": "object",
      "properties": {
        "filterField": {
          "type": "string"
        },
        "filterKey": {
          "type": "string"
        },
        "type": {
          "type": "string"
        },
        "values": {
          "$ref": "#/definitions/model.ProductSearchAttributeValueRequest"
        }
      },
      "title": "model.ProductSearchAttributeRequest"
    },
    "model.ProductSearchAttributeValueRequest": {
      "type": "object",
      "properties": {
        "imageUrl": {
          "type": "string"
        },
        "text": {
          "type": "string"
        }
      },
      "title": "model.ProductSearchAttributeValueRequest"
    }
  }
}

main func:

func main() {
	input, err := ioutil.ReadFile("min.json")
	if err != nil {
		panic(err)
	}
	oapi3, err := V2ToV3(input)
	if err != nil {
		panic(err)
	}
	_ = openapi3filter.NewRouter().WithSwagger(oapi3)
}

Output:

panic: Validating Swagger failed: Found unresolved ref: '#/definitions/model.ProductSearchAttributeValueRequest'

So, #/definitions/model.ProductSearchAttributeValueRequest is defined under definitions as we see. That simplified json is auto-generated file from Swag. I don't know why it throws an error, i think it shouldn't. Should it?

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