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][Go] Support allOf inheritance with embedded go structs #4606

Closed
4 of 6 tasks
sebastien-rosset opened this issue Nov 25, 2019 · 2 comments
Closed
4 of 6 tasks

[BUG][Go] Support allOf inheritance with embedded go structs #4606

sebastien-rosset opened this issue Nov 25, 2019 · 2 comments

Comments

@sebastien-rosset
Copy link
Contributor

sebastien-rosset commented Nov 25, 2019

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

Currently, golang structs are generated as a flat structure, even when the OAS spec uses "allOf" and discriminators. I understand generating flat structures is valid, but IMO it would be better to leverage the "supportsInheritance" attribute and generate embedded go structs, or at least have an option to generate embedded go structs. This would make the generated code less verbose, easier to read and use, reduce the size of the generated code.

openapi-generator version

4.2.1

OpenAPI declaration file content or url

https://gist.github.com/sebastien-rosset/f32f0abea11416bf82175577b48c251e

Command line used for generation
JAR_FILE=openapi-generator-cli.jar
config=config.yaml
spec=vehicles.yaml
java -jar ${JAR_FILE} generate \
   -c $config \
   -i ${spec} \
   -g go-experimental \
   -o out \
   --enable-post-process-file
Steps to reproduce
  1. Checkout OpenAPITool from master and build it
  2. Generate go client code from the OAS spec: https://gist.github.com/sebastien-rosset/f32f0abea11416bf82175577b48c251e
  3. Notice the model files are flattened and do not use supportsInheritance

Also, the AbstractGoCodegen.java file does not set the "supportsInheritance" field to true, unlike the AbstractJavaCodegen:
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractJavaCodegen.java#L97

Related issues/PRs

PR to inherit the discriminators from all ancestors:
#4454

Issue with proposal to use interfaces:
#4559

Suggest a fix

Strawman proposal:
Set the "supportsInheritance" attribute to true for golang code.
In model.mustache, generate embedded go structs for the parent.

Potentially, there could be a flag to control whether or not the generated classes should be flattened.

@mariuspavelnistor
Copy link

Hi Sebastien, I'm trying to use this feature but it doesn't embed the parent structure into the child. I'm using open-api-generator 4.3.1 . I changed the mustache also. Is anything else I can do ?

@sebastien-rosset
Copy link
Contributor Author

sebastien-rosset commented Oct 2, 2020

Hi Sebastien, I'm trying to use this feature but it doesn't embed the parent structure into the child. I'm using open-api-generator 4.3.1 . I changed the mustache also. Is anything else I can do ?

You use the go-experimental target, not go, right? This feature is not available in go and it's not available in the go server.
Also note that in 5.0, the go target is renamed to go-deprecated and go-experimental is renamed to go.

I am closing this issue this it was fixed with #4607
Can you verify #4607 was merged before 4.3.1 was released?

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

2 participants