OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
22k stars 6.6k forks source link

[BUG][JAVA] Fluent setters from inherited from parent return parents type #14915

Closed leonard84 closed 1 year ago

leonard84 commented 1 year ago

Bug Report Checklist

Description

When using inheritance via allOf the setters always return the type they are declared on, so using setters declared on the parent type will return Parent instead of Child.

openapi-generator version

6.4.0

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  title: test
  description: >-
    test schema
  version: 1.0.0
servers:
  - url: 'http://test.com'
    description: stage
paths:
  /demo:
    get:
      summary: placeholder summary
      responses:
        '200':
          description: Success
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Child'
components:
  schemas:
    Parent:
      type: object
      properties:
        name:
          type: string
    Child:
      x-all-of-name: Child
      allOf:
        - $ref: '#/components/schemas/Parent'
        - type: object
          properties:
            childProperty:
              type: string
Generation Details

https://github.com/gradle/gradle-enterprise-api-samples/blob/5b9a67556042413337860c47532512046f8765a0/build.gradle.kts#L35-L57

Steps to reproduce

Checkout the demo at https://github.com/gradle/gradle-enterprise-api-samples/tree/openapi-generator-issue_inheritance and run build

Will fail with:

error: incompatible types: demo.api.model.Parent cannot be converted to demo.api.model.Child
        Child child = new Child().name("bar");
                                      ^
Related issues/PRs
Suggest a fix

The straight forward solution would be to @Override all inherited fluent setters on the child types, so that they return the correct type.

So on Child add this method

  @Override
  public Child name(String name) {    
    this.name = name;
    return this;
  }
borsch commented 1 year ago

@leonard84 if you using lombok in your project you can fix this by adding @lombok.SuperBuilder to additionalModelTypeAnnotations

leonard84 commented 1 year ago

@borsch thanks for the hint, but we don't use lombok.