express-openapi-validator
express-openapi-validator copied to clipboard
Can't resolve reference on valid spec
Describe the bug
I get an error sending a request into my express api. I have validated my schema and also the schema generated from @apidevtools/json-schema-ref-parser and both are valid. When I use @apidevtools/json-schema-ref-parser to generate a json bundle, using $refParser.bundle, the schema produced has an internal link from updatedAt to createdAt, which seems to be the issue.
To Reproduce Use the following schema:
openapi.yaml
---
openapi: 3.0.0
info:
version: 1.0.0
title: My Service
paths:
/test:
get:
operationId: GetTest
responses:
'200':
description: OK.
content:
application/json:
schema:
$ref: '#/components/schemas/ReturnData'
components:
schemas:
ReturnData:
type: object
required:
- createdAt
properties:
createdAt:
$ref: './types.yaml#/components/schemas/DateTime'
updatedAt:
$ref: './types.yaml#/components/schemas/DateTime'
types.yaml
---
openapi: 3.0.0
info:
version: 0.1.0
title: Date/Time Types
paths: {}
components:
schemas:
DateTime:
type: string
format: date-time
If I modify the openapi.yaml file and remove the reference to the types.yaml file and hard-code the date-time format everything works as expected.
Actual behavior Error: can't resolve reference #/components/schemas/ReturnData/properties/createdAt from id #/components/schemas/ReturnData
Expected behavior Validation successful
Generated by $refParser.bundle
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "My Service"
},
"paths": {
"/test": {
"get": {
"operationId": "GetTest",
"responses": {
"200": {
"description": "OK.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ReturnData"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"ReturnData": {
"type": "object",
"required": [
"createdAt"
],
"properties": {
"createdAt": {
"type": "string",
"format": "date-time"
},
"updatedAt": {
"$ref": "#/components/schemas/ReturnData/properties/createdAt"
}
}
}
}
}
}