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

Unable to use readOnly and writeOnly for a property and have it validated #114

Open
vinujohn opened this issue Nov 25, 2020 · 4 comments
Open

Comments

@vinujohn
Copy link

vinujohn commented Nov 25, 2020

https://swagger.io/docs/specification/data-models/data-types/#readonly-writeonly

Depending on where the property is defined (request vs. response) I would expect the validator to account for readOnly and writeOnly fields. I would also expect that if a property is marked as "required", that property would not be validated for being required if either it showed up in the response as writeOnly or in the request as readOnly.

@codekie
Copy link
Owner

codekie commented Nov 25, 2020

What this library does is, it extracts the examples out of the OpenAPI-description, creates a map between the examples and the corresponding schema and then validates the examples against the schema, via JSON-schema-validator. Its context (request, response) are not taken into consideration, at the moment and it also could not be validated with the JSON-schema-validator but would have to be checked additionally. This is a not so trivial change. I'll add it on the todo-list, but it may take a while to implement this.

@bartoszm
Copy link
Contributor

bartoszm commented Sep 26, 2023

I will add a reference to the OAS 3.0 expected behavior.

Relevant only for Schema "properties" definitions. Declares the property as “read only”. This means that it MAY be sent as part of a response but SHOULD NOT be sent as part of the request. If the property is marked as readOnly being true and is in the required list, the required will take effect on the response only. A property MUST NOT be marked as both readOnly and writeOnly being true. Default value is false.

https://spec.openapis.org/oas/v3.0.0#fixed-fields-19

ajv seems not to support readOnly / writeOnly: ajv-validator/ajv#2097

@bartoszm
Copy link
Contributor

I did some digging and it seems support for read/write only might be relatively easy. At least for examples in paths.
The OAS spec is fully dereferenced so you could modify the model in place in the following way:
a) response models:

  • you need to remove all writeOnly attributes,
  • you need to remove these attribute's names from required
    b) request model
  • you need to remove all readOnly attributes,
  • you need to remove these attribute's names from required
    you need to set additionalProperties to false for these models.

For composed models, it is a bit more tricky. Searching for 'required must consider allOf, and anyOf. Setting additionalProperties won't work.

@codekie
Copy link
Owner

codekie commented Sep 28, 2023

As you wrote in #114 (comment), ajv does not support readOnly and writeOnly. Thus, everything mentioned in #114 (comment) still applies, starting with

... Its context (request, response) are not taken into consideration, at the moment ...

The specs to the to-validate examples are determined after the examples have been collected. At this point it is not known (yet) whether it is a request- or response-spec, also at this point modifying the spec (removing / adding / setting properties) is not intended and would require not-so-trivial changes in the core and extensive testing to ensure that everything else still works as before.

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

No branches or pull requests

3 participants