openapi-backend
openapi-backend copied to clipboard
Error: "nullable" cannot be used without "type"
I'm generating the following yaml spec file with openapi 3.0.0 from my kotlin backend:
Request:
required:
- field1
- field2
- field3
type: object
properties:
field1:
type: string
field2:
type: string
format: date-time
field3:
type: string
field4:
allOf:
- $ref: '#/components/schemas/RequestVehicleConfiguration'
- nullable: true
The interesting part here is this field4
which may be a nulllable reference.
After that I try to write a test and mock this API:
const api = new OpenAPIBackend({ definition: this.spec });
// Register some handlers
api.init()
Expected behaviour: Api is initialized
Actual behaviour: Error: "nullable" cannot be used without "type"
What I tried so far:
- Tried to use
openapi-backend
4.1.0
and4.2.0
I tried to replace
field4:
allOf:
- $ref: '#/components/schemas/RequestVehicleConfiguration'
- nullable: true
with
vehicleConfiguration:
$ref: '#/components/schemas/RequestVehicleConfiguration'
And it helps but I don't won't to do it manually, I would like to rely on automatic schema generation. Any ideas what I'm doing wrong? As far as I understand the schema is valid, why I cannot generate an API mock from it?
Just dropping this here:
https://github.com/OAI/OpenAPI-Specification/issues/1368
Might be a feature 'support openapi 3.1'
FWIW - I'm using OpenAPI Backend to implement an application using a spec developed by a standards organization. They recently switched to using nullable: true
, for which I'm getting this sort of error:
/Volumes/Evoke/esx-server/server/node_modules/ajv/lib/compile/validate/dataType.ts:26
throw new Error('"nullable" cannot be used without "type"')
^
Error: "nullable" cannot be used without "type"
at getSchemaTypes (/Volumes/Evoke/esx-server/server/node_modules/ajv/lib/compile/validate/dataType.ts:26:13)
at typeAndKeywords (/Volumes/Evoke/esx-server/server/node_modules/ajv/lib/compile/validate/index.ts:159:31)
at subSchemaObjCode (/Volumes/Evoke/esx-server/server/node_modules/ajv/lib/compile/validate/index.ts:147:3)
at subschemaCode (/Volumes/Evoke/esx-server/server/node_modules/ajv/lib/compile/validate/index.ts:124:7)
at KeywordCxt.subschema (/Volumes/Evoke/esx-server/server/node_modules/ajv/lib/compile/validate/index.ts:500:5)
at applyPropertySchema (/Volumes/Evoke/esx-server/server/node_modules/ajv/lib/vocabularies/applicator/properties.ts:45:11)
at Object.code (/Volumes/Evoke/esx-server/server/node_modules/ajv/lib/vocabularies/applicator/properties.ts:32:9)
at keywordCode (/Volumes/Evoke/esx-server/server/node_modules/ajv/lib/compile/validate/index.ts:532:9)
at /Volumes/Evoke/esx-server/server/node_modules/ajv/lib/compile/validate/index.ts:265:9
at CodeGen.code (/Volumes/Evoke/esx-server/server/node_modules/ajv/lib/compile/codegen/index.ts:525:33)
Commenting out all uses of nullable: true
fixed the problem. I see the link provided by @jaecktec but in this case it is not associated with $ref
uses.
Some examples:
x:
type: number
format: float
description: A value on an x axis.
example: 1.0
# nullable: true
default: null
targets:
type: array
description: An list of target objects.
# nullable: true
default: null
items:
$ref: '#/components/schemas/target'
userIdentifier:
oneOf: [
{type: integer},
{type: string}
]
description: User generated numeric or string identifier
example: 99
# nullable: true
default: null
programDescriptions:
type: array
description: A list of programDescriptions
# nullable: true
default: null
items:
required:
- URL
properties:
URL:
type: string
# format: uri
description: a human or machine readable program description
example: www.myCorporation.com/myProgramDescription
The last one also demonstrates another unrelated issue - that format: uri
is not recognized.