OpenAPITools / OpenAPITools/openapi-generator

[BUG] [JAVA] Using allOf to wrap a $ref as an array item generates a different object.

Open
#12,717 0 comments 12 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

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

OAS spec = v3.0.3

With the version 6.0.0, when wrapping a $ref with allOf on an array item, a new model is generated. That new model is the one used as the object type for the array instead of the original being referenced by $ref.

Note: We wrapped the $ref with allOf so that we can add sibling "xml.name"

5.4.0 behavior (expected):

public class Errors   {

  @JsonProperty("errors")
  @JacksonXmlProperty(localName = "errors")
    @XmlElement(name = "error")
  @Valid
  private List<Error> errors = new ArrayList<>();

6.0.0 behavior:

public class Errors   {

  @JsonProperty("errors")
  @JacksonXmlProperty(localName = "errors")
    @XmlElement(name = "errors")
  @Valid
  private List<ErrorsErrorsInner> errors = new ArrayList<>();
openapi-generator version

6.0.0

OpenAPI declaration file content or url
schemas:
    Errors:
      description: The list of errors related to the API
      type: object
      required:
        - errors
      properties:
        errors:
          type: array
          items:
            allOf:
              - $ref: '#/components/schemas/Error'
            xml:
              name: 'error'
      xml:
        wrapped: true
        name: 'errors'
    Error:
      description: A detailed error related to the API
      type: object
      properties:
        code:
          description: The error code
          type: string
          example: '1111111'
        message:
          description: A human-readable description of the error for debugging purposes
          type: string
          example: 'The email has not been provided'
        field:
          description: The name of the field provided in the original request (if any) that the error pertains to
          type: string
          example: 'email'
      xml:
        name: 'error'
Generation Details

We are using openapi-generator-maven-plugin on a java project.
-- mvn clean install

Steps to reproduce
  • Create an object and add an array as a paramter
  • Reference another object as the array item and wrap it with allOf
Related issues/PRs
Suggest a fix

OAS 3.1.0 allows $ref to have sibling which eliminates the need to wrap it with allOf... but is not yet supported by openapi generator as far as I know.

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 the provided OAS 3.0.3 schema and the openapi-generator-maven-plugin, then run the documented mvn clean install reproduction. Compare the generated Errors model with the expected Java snippet; done means the array uses Error and preserves the XML item name error rather than generating ErrorsErrorsInner with errors.

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
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.