OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVA] microprofile client generator skip setters for readOnly properties

Open
#10,086 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

If I have the following schema in my swagger file:

Swagger snippet
"ResourceId": {
        "description": "API: Identifier assigned by the ASPSP for further use of the created resource through API calls\n",
        "type": "string",
        "pattern": "^([a-zA-Z0-9 /\\-?:\\()\\.,']{1,35})$",
        "readOnly": true
      },
"AccountIdentification": {
        "description": "Unique and unambiguous identification for the account between the account owner and the account servicer.",
        "type": "object",
        "properties": {
          "resourceId": {
            "$ref": "#/components/schemas/ResourceId"
          },
        "example": {
          "resourceId": "BE22222222222222"
        }
      },

Then using the following configOptions for the java generator maven plugin confiugration:

                            <configOptions>
                                <library>microprofile</library>
                                <dateLibrary>java8</dateLibrary>
                            </configOptions>

The following pojo is generated:

AccountIdentification.java
public class AccountIdentification  {


 /**
   * API: Identifier assigned by the ASPSP for further use of the created resource through API calls 
  **/
  private String resourceId;

 /**
   * API: Identifier assigned by the ASPSP for further use of the created resource through API calls 
   * @return resourceId
  **/
  @JsonbProperty("resourceId")
  public String getResourceId() {
    return resourceId;
  }

// No setter
  

and the following snippet fails:

       Jsonb jsonb = JsonbBuilder.create();
        String resourceJson = "{\"resourceId\": \"test\"}";
        AccountIdentification parsed = jsonb.fromJson(resourceJson, AccountIdentification.class);
        System.out.println(parsed.getResourceId());

        String formatted = jsonb.toJson(parsed);
        System.out.println(formatted);

      Assertions.assertEqual("test", parsed.getResourceId());

output:

null
{}

Setting readOnly to false generates the setter and fixes the serialization.
I am expecting to be able (de)serialize generated models using jsonb, regardless of the readOnly property.

openapi-generator version
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>5.2.0</version>
Suggest a fix

I think all setters (regardless of readOnly in the schema), or a @JsonbCreator constructor with all Properties should be generated.

    @JsonbCreator
    public AccountIdentification(@JsonbProperty("resourceId") String resourceId) {
        this.resourceId = resourceId;
    }

The openapi spec mentions regarding readOnly: ' This means that it MAY be sent as part of a response but SHOULD NOT be sent as part of the request', so the restriction is not strict, and the payload needs to be deserialized if it is part of the response anyway.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Java generator's microprofile library generation path and its handling of readOnly properties, then reproduce the issue with the provided AccountIdentification schema and Jsonb snippet. Done means generated models can deserialize and serialize readOnly properties while preserving the schema's intended request and response semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.