Search code examples
goswaggerswagger-editorgo-restful

go-restful-openapi $refs must reference a valid location in the document


I'm using go-restful in combination with go-restful-openapi to generate my swagger doc automatically. However, on testing the tool with the swagger-editor, I get the following error:

$refs must reference a valid location in the document

struct

type Users struct {
  # uuid imported from github.com/google/[email protected]
  RelatedUsers []uuid.UUID `json:"relatedIds" validate:"required"`
}

generated swagger snippet

  "Users": {
   "required": [
    "relatedIds"
   ],
   "properties": {
    "relatedIds": {
     "type": "array",
      "$ref": "#/definitions/uuid.UUID" #this line returns an error 
     }
    }
   }
  }

Here's the swagger config:

    swaggerConfig := restfulspec.Config{
        WebServices: restfulContainer.RegisteredWebServices(),
        APIPath:     "/swagger.json",
        PostBuildSwaggerObjectHandler: func(swo *spec.Swagger) {
            swo.Info = &spec.Info{
                InfoProps: spec.InfoProps{
                    Title:       "User Service",
                    Description: "An example service for stackoverflow",
                    Version:     "1.0.0",
                },
            }
        },
    }

NOTE: If I replace the lines above in the swagger editor as follows the error disappears, however, I couldn't figure out how to configure swagger to do so automatically

  "Users": {
   "required": [
    "relatedIds"
   ],
   "properties": {
    "relatedUsers": {
        type: array
        items:
         type: "string"
         format: "uuid"
     }
    }
   }
  }

Solution

  • Just in case someone is running into this issue: go-restful-openapi cannot resolve imported custom types. To solve this issue add the type to the definitions

    swo.Definitions["uuid.UUID"] = spec.Schema{
        SchemaProps: spec.SchemaProps{
                    Type:   []string{"string"},
                    Format: "uuid",
        },
    }