oas3-chow-chow
oas3-chow-chow copied to clipboard
readOnly not working as expected
When adding readonly: true to a schema, an error is thrown by oas3-chow-chow:
'TypeError: Cannot read properties of undefined (reading 'schemaContext')\n
at Ajv.validate (<path>/node_modules/oas3-chow-chow/src/compiler/CompiledSchema.ts:22:8)\n
at CompiledSchema.validate [as validator] (eval at localCompile (<path>/node_modules/ajv/lib/compile/index.js:120:26), <anonymous>:3:1785)\n
at CompiledSchema.validate (<path>/node_modules/oas3-c…edPath.validateRequest (<path>/node_modules/oas3-chow-chow/src/compiler/CompiledPath.ts:49:34)\n
I believe it's somewhere in this code, but I'm not sure what the code is doing. I see the code comment "Remove unsupported additional OpenAPI keywords." and what looks to be a redefining of the validation function?
thanks
Hey @crizo23, by default the readonly and writeonly are metadata keywords, and AJV by default doesn't perform any validation on it. See https://ajv.js.org/json-schema.html#metadata-keywords
What we did in the code there was that, we added the keyword validation by saying we only allow "readonly" in a request schema and "writeonly" in a response schema. Let me know what's your opinion on that.
And in your cases, I suspect that you have put readonly on other part of the schema, like header or cookie for example which would cause the context to be undefined. To quickly fix it, we can make it so that it alway return true when the context isn't available. WDYT?
@supertong yes you're right, in our case it was on a path param, but I think this was a mistake on our part. In other words, readonly:true on a path param doesn't really make semantic sense (IMO). My interpretation of readonly was that it was meant to be used in a schema object, typically in a request/response. So, as you have it implemented makes sense, and indeed I was able to get it working in a request schema
I think your solution makes sense. The result of it returning true would basically be like it ignoring readonly:true on a path param (or header, cookie, etc), do I understand correctly?