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

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

Closed
Dentrax opened this issue Mar 10, 2020 · 4 comments · Fixed by #237
Closed

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

Dentrax opened this issue Mar 10, 2020 · 4 comments · Fixed by #237

Comments

@Dentrax
Copy link

Dentrax commented Mar 10, 2020

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": "[email protected]"
    }
  },
  "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?

@fenollp
Copy link
Collaborator

fenollp commented Mar 10, 2020

Could you print out oapi3?

@Dentrax
Copy link
Author

Dentrax commented Mar 10, 2020

@fenollp

oapi3.MarshalJSON() output:

{"components":{"schemas":{"model.ProductSearchAttributeRequest":{"properties":{"filterField":{"type":"string"},"filterKey":{"type":"string"},"type":{"type":"string"},"values":{"$ref":"#/definitions/model.ProductSearchAttributeValueRequest"}},"title":"model.ProductSearchAttributeRequest","type":"object"},"model.ProductSearchAttributeValueRequest":{"properties":{"imageUrl":{"type":"string"},"text":{"type":"string"}},"title":"model.ProductSearchAttributeValueRequest","type":"object"}}},"info":{"contact":{"email":"[email protected]","name":"Test"},"description":"Test Golang Application","title":"Test","version":"1.0"},"openapi":"3.0"}

@fenollp
Copy link
Collaborator

fenollp commented Mar 10, 2020

Seems refs do not get properly rewritten when converting from v2 to v3:

components:
  schemas:
    model.ProductSearchAttributeRequest:
      properties:
        values:
          "$ref": "#/definitions/model.ProductSearchAttributeValueRequest"
      ...

Note how the components and /definitions/ don't match anymore.

Something fishy seems to be happening around

schema.Value.Properties[k] = ToV3SchemaRef(v)

Note that this function is not converting allOf oneOf anyOf and not keys either.
I'll gladly review any PR sent my way :)

@Dentrax
Copy link
Author

Dentrax commented Mar 11, 2020

Still getting same Found unresolved ref: '#/components/schemas/model.ProductSearchAttributeValueRequest'. JSON output:

components:
  schemas:
    model.ProductSearchAttributeValueRequest:
      ...
    model.ProductSearchAttributeRequest:
      properties:
        values:
          "$ref": "#/components/schemas/model.ProductSearchAttributeValueRequest"
      ...

Input JSON:

{
  "swagger": "2.0",
  "info": {
    "description": "Test Golang Application",
    "version": "1.0",
    "title": "Test",
    "contact": {
      "name": "Test",
      "email": "[email protected]"
    }
  },
  "host": "",
  "basePath": "/test",
  "definitions": {
    "model.ProductSearchAttributeValueRequest": {
      "type": "object",
      "properties": {
        "text": {
          "type": "string"
        }
      },
      "title": "model.ProductSearchAttributeValueRequest"
    },
    "model.ProductSearchAttributeRequest": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string"
        },
        "values": {
          "$ref": "#/components/schemas/model.ProductSearchAttributeValueRequest"
        }
      },
      "title": "model.ProductSearchAttributeRequest"
    }
  }
}

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

Successfully merging a pull request may close this issue.

2 participants