''can't resolve reference #/components/schemas/<SomeSchema> from id #'

I'm trying to validate my requests thru my OpenAPI schema with the openapi-request-validator package.

However, everytime I try to test a request against the schema, I manage to match it to the right path, the right resource, but it fails by returning this error: MissingRefError: can't resolve reference #/components/schemas/CreateTechnicalAssetRequest from id #

Here is the relevant code snippet:

const getParams = (schema: { paths: { [x: string]: any } }, resource: string | number, method: string) => {
  const methods = schema.paths[resource]
  if (!methods) {
    throw Error(`The resource doesn't exist in the swagger definition`)
  }
  const params = methods[method.toLowerCase()]
  if (!params) {
    throw Error(`The method for the selected resource doesn't exist in the swagger definition`)
  }
  if (!params.parameters) {
    params.parameters = []
  }
  return params
}
const validateRequest: any = async (event: APIGatewayEvent, schemaPath: any) => {
  try {
    const { resource, httpMethod, queryStringParameters, pathParameters } = event
    const requestData = {
      body: getBody(event),
      headers: getHeaders(event),
      params: removeEmptyValues(pathParameters || {}),
      query: queryStringParameters || "",
    }
    const schema = yamljs.load(path.join(__dirname, "../", process.env.SWAGGER_SCHEMA))
    const params = getParams(schema, resource, httpMethod)
   // this line fails and returns the error.
    const validator = new RequestValidator(params)
    return validator.validate(requestData)
  } catch (errors) {
    return { errors }
  }
}

Now, I do not understand what this error means, I've checked whether OpenAPI json is valid and it is. My schema is the following:

{
  "openapi": "3.0.0",
  "info": {
    "description": " Marketplace Backend API definition.",
    "version": "1.0.0",
    "title": "OpenAPI documentation for the  Marketplace"
  },
  "paths": {
    "/businessAsset/{businessAssetId}/technicalAssets": {
      "post": {
        "tags": ["Technical assets"],
        "summary": "Create a technical assets associated to a business Asset",
        "operationId": "create-businessAsset-technicalAssets",
        "security": [
          {
            "TokenAuthorizer": []
          }
        ],
        "parameters": [
          {
            "in": "path",
            "name": "businessAssetId",
            "description": "Business Asset for which the technical assets need to be retrieved",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "requestBody": {
          "description": "Information of the technical asset",
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CreateTechnicalAssetRequest"
              }
            }
          }
        },
        "responses": {
          "201": {
            "description": "successful operation"
          },
          "400": {
            "$ref": "#/components/responses/BadRequest"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "CreateTechnicalAssetRequest": {
        "type": "object",
        "required": [
          "name",
          "shortDescription",
          "sourceType",
          "assetType",
          "assetDeploymentType"
        ],
        "properties": {
          "name": {
            "type": "string",
            "description": "The name of the technical asset"
          },
          "shortDescription": {
            "type": "string",
            "description": "Short, plain-text, description or the technical asset"
          },
          "sourceType": {
            "type": "string",
            "enum": ["githubRelease", "githubBranch", "zipFile"]
          },
          "assetType": {
            "type": "string",
            "enum": [
              "dataset",
              "modelServing",
              "modelTraining",
              "pythonNotebook"
            ]
          },
          "assetDeploymentType": {
            "type": "string",
            "enum": ["infrastructureAsCode", "downloadableArchive"]
          },
          "buildPackageFramework": {
            "type": "string",
            "enum": ["serverless.com"]
          },
          "supportedClouds": {
            "type": "array",
            "items": {
              "type": "string",
              "enum": ["openpaas", "azure", "aws", "gcp"]
            }
          },
          "githubRepositoryURL": {
            "type": "string",
            "description": "The url of the github repository where the technical asset shouild be retrieved"
          },
          "githubRepositoryBranch": {
            "type": "string",
            "description": "Name of the branch to use to retrieve the asset"
          },
          "githubRepositoryRelease": {
            "type": "string",
            "description": "The version of the github release where the technical asset should be retrieved."
          }
        }
      }
    }
  },
  "externalDocs": {
    "description": "Find out more about Swagger",
    "url": "http://swagger.io"
  }
}

Can someone guide around this error? Am I misunderstanding something?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source