Repository navigation
Description
Is your feature request related to a problem? Please describe.
Yes, missing support for OAS 3.0 feature of readOnly and writeOnly properties within a model.
Discussion came up on Slack, and I'm documenting it here since we need to implement this for full OAS 3.0 support.
Describe the solution you'd like
Provide a writeOnlyVars to match other vars in our codegen model. Support differentiation between request models and response models.
Describe alternatives you've considered
Early discussions about writeOnly on OAI suggest domain modeling around usage of allOf. See OAI/OpenAPI-Specification/issues/425.
Additional context
OAS 3.x Schema Object supports two properties, readOnly and writeOnly. These properties are defined as:
We should validate that a property is not both readOnly=true and writeOnly=true. This validation should be excluded from strict spec skipping as having a property both readOnly and writeOnly should be a logical error with undefined behavior.
The use of SHOULD NOT in the spec is defined via rfc2119:
SHOULD NOT This phrase, or the phrase "NOT RECOMMENDED" mean that
there may exist valid reasons in particular circumstances when the
particular behavior is acceptable or even useful, but the full
implications should be understood and the case carefully weighed
before implementing any behavior described with this label.
So, we're not technically out of compliance with the spec in relation to readOnly and writeOnly since our use case to support OpenAPI 2.0 and OpenAPI 3.0 functionality in the same generator + template combinations could be considered a "particular circumstance". However, the we are out of compliance with the clause regarding required because this makes the property required only in a request or response structure, and not both.
My proposal is to do multiple passes on models defined in the OpenAPI result object and sort these into an array of "RequestModels" and an array of "ResponseModels", leaving our current "models" collection as a raw collection for backward compatibility. This would allow us to have the same schema definition defined appropriately (according to the specification), without hacky workarounds like adding arbitrary prefixes or suffixes to differentiate.
We may be tempted to suggest that users split their schemas, similar to the recommendation linked above (OAI/OpenAPI-Specification/issues/425), but this will only address the issue for those users who own/manage their specifications. To allow code generation from external systems which follow OpenAPI 3.0 specification, we'd need to support this use case.
Activity
[-][REQ] Feature Request Description[/-][+][REQ] OAS 3.0 support of readOnly/writeOnly modeling[/+]bodograumann commentedon Nov 7, 2019
I would also love to see this feature.
From the typescript perspective we are currently simply exporting all models. So if there will be one “RequestModel” and one “ResponseModel” (which I think makes sense), the exports will have to be handled differently.
We will definitely first have to rewrite the typescript generator, in order not to have to implement it in all of them.
sebastien-rosset commentedon Mar 11, 2020
FYI, the python-experimental client does serialize read-only properties. I think it should skip serialization of the readOnly properties.
@spacether, what do you think?
spacether commentedon Mar 11, 2020
@sebastien-rosset this could be added to python-experimental the following way:
I have a general question though. Don't codegen parameters include the readonly and writeonly properties? Why do we need the new lists in Codegenmodel if we already have this info?
sebastien-rosset commentedon Mar 11, 2020
One consideration is attributes beyond read only and write-only, maybe through vendor extensions. For example, we use read-on-create for cases such as when the backend generates an API key secret and returns the value only when the resource is created. We also have create-only for properties that can be set during creation but cannot subsequently be modified.
Instead of creating lists of readonly_vars and writeonly_vars, can this be done using meta-data for each property? I.e. in the case of python, you already generate the "openapitype" meta-data for each property. Couldn't we add one more type of meta-data there?
spacether commentedon Mar 11, 2020
Thanks for sharing that context. I wasn't aware of those use cases.
So far each CodegenProperty metadata has been stored separately. A helpful refactor in python-experimental would be to create some class like CodegenProperty and gather all the data for each parameter there. Then in ModelNormal/ModelComposed we can store a dict where the key is the variable name and the value is the CodegenProperty class instance. One could also do this with just a dict to begin with.
Wouldn't that read on create and create only vendor extension need to be individually handled by generators? I am worried about us adding too many lists of vars in CodegenModel.
spacether commentedon Mar 11, 2020
Though looking at CodegenModel we already have readOnlyVars, so sure, let's add writeOnlyVars if we have already started going down this path.
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenModel.java#L61
sebastien-rosset commentedon Mar 11, 2020
I like this.
It seems that instead of maintaining separate lists, each property could have a list of meta-data attributes, and the generated code could look up the meta-data. For example, it could decide what to do for the serialization. If we want to get fancy, we could also expose a callback to the application such that app developers can insert their custom serialization logic.
last-partizan commentedon Sep 21, 2020
Is there any roadmap for this issue?
I'm using typescript-axios generator and it generates invalid code, which requires read-only params for request.
MLeipziger commentedon Aug 17, 2021
Is there any update on this issue? As mentioned by @last-partizan this generates invalid code.
rdelcampog commentedon Dec 29, 2021
Need this :(
edy-rodrigues commentedon Apr 14, 2022
I need this too!
spacether commentedon Apr 15, 2022
One can also do this with composition instead of readonly/writeOnly.
Define one schema with shared properties.
Define another for readonly and allOf include the shared schema and define readonly additions.
Define another for the writeOnly schema in the same way. This way it is deterministic which properties are in the readonly and writeOnly schemas.
One can have non deterministic behavior by using readonly. Here is an example:
A:
Properties:
a:
ReadOnly
B:
properties:
a:
writeOnly
C:
AllOf:
Is C.a readonly or writeOnly?
bodograumann commentedon Jul 19, 2022
Readonly and writeonly flags are now implemented for properties with simple types like
string, but not when a property has a custom model as its scheme.So we only need it for the latter as well?
creyD commentedon Nov 19, 2022
@bodograumann I believe this would be crucial for many projects as well.
naoyahieda commentedon Feb 18, 2023
This will be helpful for many developers!!
UnleashSpirit commentedon Mar 10, 2023
Damn, thought it was already good and found this issue :(
Need it for spring generator
Steeve55 commentedon Jul 20, 2023
Need this too please :)
s-jepsen commentedon Dec 22, 2023
Any progress on this issue?
jorgerod commentedon Jan 31, 2024
I think it is a very important feature.
Is this on the roadmap? @wing328 @jimschubert
bejvisek commentedon May 31, 2024
Any news on this issue?
Honestly, I don't see a way to use the API without this.
How do you handle field "ID" - it's always returned, but typically not send in "request", because it's created by the server.
daniel17903 commentedon Aug 22, 2024
Any progress on this issue?
subhanshu-shukla-ril commentedon Sep 4, 2024
Any progress on this issue?
awais786 commentedon Nov 28, 2024
I am new to this and I have a question. I have
writeonlyfield in schema. but generated model is doing validation on it and throwing error any ideas or any flag to skip this ?nicotsx commentedon Sep 26, 2025
Has someone found a way around this? We are using an automated tool to generate the openapi schema and the said tool is using
writeOnlyandreadOnlyso we are currently blocked because the generated clients are validating values that are not returned by the APIAs per the specs:
JSamir commentedon Oct 13, 2025
I have added this to my openapi spec as a workaround with jackson:
x-field-extra-annotation: '@com.fasterxml.jackson.annotation.JsonProperty(access = com.fasterxml.jackson.annotation.JsonProperty.Access.WRITE_ONLY)'Is there any drawbacks to using this until this feature is implemented?