OpenAPITools / OpenAPITools/openapi-generator

readonly fields in parents generate invalid code with 'microprofile' generator

Open
#23,034 0 comments 0 reactions 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

When using allOff and having readonly fields in the parent, then the 'microprofile' generator creates a constructor that does not compile:

@JsonbCreator
public Child(
@JsonbProperty(value = "parentField") String parentField
) {
this.parentField = parentField; //As parentField is private, this is not allowed.
}

openapi-generator version

7.19.0

OpenAPI declaration file content or url
info:
  title: Reproduce
  version: 'v1'
components:
  schemas:
    Child:
      required:
        - childField1
      type: object
      allOf:
        - $ref: "#/components/schemas/Parent"
      properties:
        childField1:
          type: string

    Parent:
      required:
        - parentField
      type: object
      properties:
        parentField:
          type: string
          readOnly: true
      discriminator:
        propertyName: classType
        mapping:
          Child: "#/components/schemas/Child"
paths:
  '/v1/reproduce':
    get:
      responses:
        '200':
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Child'
          description: 'reproduce'
Generation Details
generatorName = "java"
library = "resteasy"
...

configOptions = [
        generatorName       : "java",
        library             : "microprofile",
]
Steps to reproduce

Just generate the code.

Related issues/PRs

#16374 but for generator 'resteasy'

Suggest a fix

The bug is caused by this line https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache#L70.

A fix could be to either call the constructor in the superclass with the parents readonly fields, or by changing the access modifiers from 'private' to 'package' on the readonly parent fields.

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 with modules/openapi-generator/src/main/resources/Java/libraries/microprofile/pojo.mustache at the referenced line, then generate Java code from the provided OpenAPI declaration using the microprofile library. Check the Child constructor and Parent readonly field access in the generated output. Done means the generated code compiles without breaking the readonly parent-field behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.