swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

readOnly field shouldn't have a set property with Java CodeGen

Open SolaceMike opened this issue 9 years ago • 9 comments

If you have a field marked as readOnly in your schema, the Java code generator still renders a set Method. For example: "uri": { "description": "URI of the object", "type": "string", "readOnly": true }

And the generator gives me: @ApiModelProperty(value = "URI of the object") @JsonProperty("uri") public String getUri() { return uri; } public void setUri(String uri) { this.uri = uri; }

A read only field should not be settable.

This is using the Swagger Editor Version 2.9.8 online at http://editor.swagger.io/#/

SolaceMike avatar Feb 19 '16 15:02 SolaceMike

@SolaceMike yes, agree that it should not have a setter if the property is readOnly.

May I know critical is this feature in your use case?

wing328 avatar Feb 21 '16 14:02 wing328

Not urgent. I just wanted to raise it for completeness. Do you happen to know if the other (non java) code gens do anything different than the java one does?

SolaceMike avatar Feb 22 '16 13:02 SolaceMike

Other code gens do not have readOnly support and we just updated C# code gen to support it: https://github.com/swagger-api/swagger-codegen/pull/2199

wing328 avatar Feb 22 '16 14:02 wing328

Don't forget that a "readOnly" property should only indicate that the client can read this value from the server but don't have the right to send a new value. I agree it could be a good thing to remove the setters, but only for the "Client" codegens. The server *HAVE TO be able to set a value.

hiveship avatar Mar 02 '16 13:03 hiveship

Spring Server Side Code Generation - BROKEN.

Looks like this change - https://github.com/swagger-api/swagger-codegen/pull/2206 seems to have broken backward compatibility and the setter methods in Java POJO's are not getting generated for spring server code generation.

I did already log a bug on this one - https://github.com/swagger-api/swagger-codegen/issues/3793 din't hear from anyone though.

Any inputs?

rajeshkamal5050 avatar Oct 20 '16 19:10 rajeshkamal5050

IMHO, readOnly has nothing to do with immutable model properties. According to the spec, it just means that the property must not be serialized in requests. See https://github.com/OAI/OpenAPI-Specification/issues/228#issuecomment-69207084

cbornet avatar Oct 20 '16 20:10 cbornet

I am fine with both the client and server code generations do nothing w.r.t to readOnly and generate both getter/setter methods.

Currently it looks like we tried to differentiate it for the client models and the server models seems to be broken now with no setter methods getting generated for readOnly properties.

rajeshkamal5050 avatar Oct 20 '16 20:10 rajeshkamal5050

Could you please fix that server Setters are generated, otherwise read only property are unusable. Or is there a workaround?

antonsn avatar May 23 '18 10:05 antonsn

Any update on this issue

subhanshu-shukla-ril avatar Sep 04 '24 13:09 subhanshu-shukla-ril