Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Model inheritance in Java and Swift is broken in v6.0.0 #12650

Open
5 tasks done
gereons opened this issue Jun 20, 2022 · 5 comments
Open
5 tasks done

[BUG] Model inheritance in Java and Swift is broken in v6.0.0 #12650

gereons opened this issue Jun 20, 2022 · 5 comments

Comments

@gereons
Copy link

gereons commented Jun 20, 2022

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?
Description

Installed v6.0.0, noticed that base class / derived class relationships no longer show up in generated Java and Swift models.

OpenAPI declaration file content or url
components:
  schemas:
    BaseClass:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
    DerivedClass:
      type: object
      allOf:
        - $ref: "#/components/schemas/BaseClass"
        - type: object
          properties:
            derivedData:
              type: string
Steps to reproduce

Generate models using openapi-generator generate -i spec.yml -g java and openapi-generator generate -i spec.yml -g swift5 --additional-properties=useClasses=true

The resulting Java code for DerivedClass looks like this:

public class DerivedClass {
  public static final String SERIALIZED_NAME_ID = "id";
  @SerializedName(SERIALIZED_NAME_ID)
  private Integer id;
  // ...
}

and the Swift version has

public final class DerivedClass: Codable, JSONEncodable, Hashable {
  // ...
}

i.e. in both cases, information about the inheritance is lost.

In the Swift case, the base class is erroneously generated as final,

public final class BaseClass: Codable, JSONEncodable, Hashable {
  // ...
}

which prohibits inheriance, even if the code for DerivedClass were generated correctly.

@valentin-braun
Copy link

We are facing the same issue for Swift. Is there a chance to get this fixed?

@wing328
Copy link
Member

wing328 commented Dec 20, 2022

Hi all, I've filed #14172 to allow using $ref as parent in allOf with a new option called "openapi-normalizer".

Please give it a try as follows:

git clone --depth 1 -b allof-parent-type https://github.com/OpenAPITools/openapi-generator/
cd openapi-generator
mvn clean package -DskipTests -Dmaven.javadoc.skip=true
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i /Users/williamcheng/Code/openapi-generator7/modules/openapi-generator/src/test/resources/3_0/allOf_extension_parent.yaml -o /tmp/java-okhttp/ --additional-properties hideGenerationTimestamp="true" --openapi-normalizer REF_AS_PARENT_IN_ALLOF=true

@gereons
Copy link
Author

gereons commented Jan 10, 2023

Thank you for looking at this, and sorry for the late reply!

Since your PR has already been merged I ran java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i spec.yml -o gen --openapi-normalizer REF_AS_PARENT_IN_ALLOF=true --skip-validate-spec -g swift5 --additional-properties=useClasses=true using the spec file I posted above using today's master (0b8c08c) and got the same result as initially reported for Swift (where my main focus lies):

public final class DerivedClass: Codable, JSONEncodable, Hashable { ... }
and
public final class BaseClass: Codable, JSONEncodable, Hashable { ... }

i.e. a base class that can't be inherited from and a derived class that doesn't inherit from anything.

Generating Java models actually produces public class DerivedClass extends BaseClass { ... } so this aspect of the regression appears to be fixed.

@gereons
Copy link
Author

gereons commented May 3, 2023

@valentin-braun if this is still relevant to you, we've finally decided to build a custom model generator from scratch: https://github.com/onvista-media/modelgen

@valentin-braun
Copy link

@gereons Thank you for the hint. In the meanwhile, I found SwagGen which works pretty well for most of my use cases. But it is written in Swift. I would better love to see a working Swift model generation in the openapi-generator project which is mainly based on a templating approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants