Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readOnly not working as expected #147

Open
crizo23 opened this issue Nov 16, 2023 · 2 comments
Open

readOnly not working as expected #147

crizo23 opened this issue Nov 16, 2023 · 2 comments

Comments

@crizo23
Copy link
Contributor

crizo23 commented Nov 16, 2023

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

@supertong
Copy link
Collaborator

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?

@crizo23
Copy link
Contributor Author

crizo23 commented Nov 17, 2023

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants