Skip to content

[JAVA] Server and models with readOnly properties #1376

@tsiq-karold

Description

@tsiq-karold
Description

Based on the description of readOnly from https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#schema-object which states that a response may contain a read-only property but a request must not contain it, I would expect generated client code to only contain getters for such objects and generated server code to contain both getters and setters. The client side behaves as expected, the server generated code however does not allow setting such properties.

Is this a bug, a misunderstanding on my part or if neither then how are we supposed to set such properties in server code?

openapi-generator version

Master

OpenAPI declaration file content or url
{
  "swagger": "2.0",
  "info": {
    "version": "0.0.1",
    "title": "Test"
  },
  "basePath": "/",
  "paths": {
    "/": {
      "get": {
        "operationId": "theGet",
        "responses": {
          "200": {
            "description": "response",
            "schema": {
              "$ref": "#/definitions/ResponseMessage"
            }
          }
        }
      }
    }
  },
  "definitions": {
    "ResponseMessage": {
      "type": "object",
      "properties": {
        "name": {
          "type": "string",
          "readOnly": true
        },
        "value": {
          "type": "string",
          "readOnly": true
        }
      }
    }
  }
}
Command line used for generation

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i api.json -g jaxrs-jersey -o output

Steps to reproduce

Generate code as mentioned above then attempt to create and return a ResponseMessage within DefaultApiServiceImpl.theGet().

Related issues/PRs

Not that I could find.

Suggest a fix/enhancement

One possible solution is to remove the if statement around setter and builder method generation for read-only fields in the appropriate templates. In my case this would be these two statements: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaJaxRS/pojo.mustache#L33 and https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaJaxRS/pojo.mustache#L90. The POJO templates for the client are separate so this would work. However, I'm not sure it's ideal doing this in each set of templates especially if any generators share model templates between client and server.

I see there is a CodegenType enum which would indicate server vs. client. An alternative solution would be to enhance the conditionals in the templates to take the type into account assuming it's accessible from the templates or even to just remove the readOnly flag from the model prior to presenting it to the template.

Any thoughts on either approach? Happy to take a stab at submitting a fix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions