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] 3.1.0 Missing type for array when using allOf and $ref #18291

Closed
5 of 6 tasks
am-on opened this issue Apr 4, 2024 · 10 comments · Fixed by #18577
Closed
5 of 6 tasks

[BUG] 3.1.0 Missing type for array when using allOf and $ref #18291

am-on opened this issue Apr 4, 2024 · 10 comments · Fixed by #18577

Comments

@am-on
Copy link

am-on commented Apr 4, 2024

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

When using allOf and $ref, the array from the referenced object doesn't include type.

Elm

type alias Foo =
    { arrayOfStrings : List String
    }

type alias Bar =
    { arrayOfStrings : List -- Should be `List String`
    }

Golang

type Foo struct {
	ArrayOfStrings []string `json:"arrayOfStrings"`
}

type Bar struct {
	ArrayOfStrings Array `json:"arrayOfStrings"` // Should be `ArrayOfStrings []string`
}
openapi-generator version

openapi-generator-cli 7.5.0-SNAPSHOT
commit : f357be4

OpenAPI declaration file content or url
openapi: 3.1.0
info:
  title: ""
  version: ""

paths:
  /user/getInfo:
    get:
      operationId: getUserInfo
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Bar'
          description: |
            OK
      security:
        - Session: []
      x-accepts: application/json

components:
  schemas:
    Foo:
      type: object
      required:
        - arrayOfStrings
      properties:
        arrayOfStrings:
          type: array
          items:
            type: string

    Bar:
      allOf:
        - $ref: '#/components/schemas/Foo'
Generation Details

Elm:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \ 
      --input-spec openapi.yaml \
      --generator-name elm \
      --output "foo"

Golang:

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \ 
      --input-spec openapi.yaml \
      --generator-name go \
      --output "foo"
Steps to reproduce

Use the provided OpenAPI declaration file and generate code.

Related issues/PRs
Suggest a fix
@wing328
Copy link
Member

wing328 commented Apr 5, 2024

thanks for reporting the issue. i've filed #18297 to fix it

@wing328
Copy link
Member

wing328 commented Apr 5, 2024

PR merged. please pull the latest master to give it a try to use the snapshot version mentioned in the project's readme

@wing328 wing328 closed this as completed Apr 5, 2024
@am-on
Copy link
Author

am-on commented Apr 5, 2024

I'm still experiencing this bug. The related bug reported in #18290 is fixed by #18297 (so I am indeed running the correct version), but this one remains. The resulting code is same as in the first comment.


openapi-generator-cli 7.5.0-SNAPSHOT
commit : 6d10e80
built : -999999999-01-01T00:00:00+18:00
source : https://github.com/openapitools/openapi-generator
docs : https://openapi-generator.tech/

@wing328 wing328 reopened this Apr 5, 2024
@wing328
Copy link
Member

wing328 commented Apr 5, 2024

just did another test with go and looks good to me:

// checks if the Bar type satisfies the MappedNullable interface at compile time
var _ MappedNullable = &Bar{}

// Bar struct for Bar
type Bar struct {
        ArrayOfStrings []string `json:"arrayOfStrings"`
}

type _Bar Bar

not sure exactly why it's not working in your side

@am-on
Copy link
Author

am-on commented Apr 5, 2024

Hm, strange. I tested on another computer to ensure there's not some strange caching causing the issues. Still getting this bug, but not the other one.

This is how I install and run the generator:

git clone https://github.com/OpenAPITools/openapi-generator
cd openapi-generator
direnv allow
mvn clean package

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
      --input-spec openapi.yaml \
      --enable-post-process-file \
      --generator-name go \
      --output "foo"

I tested it on NixOs Linux and Macbook Air M1, using the same steps with nix in both cases.

I'll ask my coworker to see if he can reproduce this bug with the latest master.

@wing328
Copy link
Member

wing328 commented Apr 7, 2024

Sorry. I didn't test it correctly. I'm now able to repeat the issue but couldn't find a solution yet.

please revert to 3.0.1 spec for the time being

@am-on
Copy link
Author

am-on commented Apr 8, 2024

I missed this issue when reporting the bug:

There are more details about the bug there.

@wing328
Copy link
Member

wing328 commented May 6, 2024

@am-on I've filed #18577, which seems to fix the issue

diff --git a/model_bar.go b/model_bar.go
index 0f57fe5..0ed5ca1 100644
--- a/model_bar.go
+++ b/model_bar.go
@@ -21,7 +21,7 @@ var _ MappedNullable = &Bar{}

 // Bar struct for Bar
 type Bar struct {
-       ArrayOfStrings []interface{} `json:"arrayOfStrings"`
+       ArrayOfStrings []string `json:"arrayOfStrings"`
 }

 type _Bar Bar
@@ -30,7 +30,7 @@ type _Bar Bar
 // This constructor will assign default values to properties that have it defined,
 // and makes sure properties required by API are set, but the set of arguments
 // will change when the set of required properties is changed
-func NewBar(arrayOfStrings []interface{}) *Bar {
+func NewBar(arrayOfStrings []string) *Bar {
        this := Bar{}
        this.ArrayOfStrings = arrayOfStrings
        return &this
@@ -45,9 +45,9 @@ func NewBarWithDefaults() *Bar {
 }

 // GetArrayOfStrings returns the ArrayOfStrings field value
-func (o *Bar) GetArrayOfStrings() []interface{} {
+func (o *Bar) GetArrayOfStrings() []string {
        if o == nil {
-               var ret []interface{}
+               var ret []string
                return ret
        }

@@ -56,7 +56,7 @@ func (o *Bar) GetArrayOfStrings() []interface{} {

 // GetArrayOfStringsOk returns a tuple with the ArrayOfStrings field value
 // and a boolean to check if the value has been set.
-func (o *Bar) GetArrayOfStringsOk() ([]interface{}, bool) {
+func (o *Bar) GetArrayOfStringsOk() ([]string, bool) {
        if o == nil {
                return nil, false
        }
@@ -64,7 +64,7 @@ func (o *Bar) GetArrayOfStringsOk() ([]interface{}, bool) {
 }

 // SetArrayOfStrings sets field value
-func (o *Bar) SetArrayOfStrings(v []interface{}) {
+func (o *Bar) SetArrayOfStrings(v []string) {
        o.ArrayOfStrings = v
 }

@am-on
Copy link
Author

am-on commented May 6, 2024

Tested #18577. It fixes the issue for me.

@wing328
Copy link
Member

wing328 commented May 7, 2024

thanks for testing the fix, which has been merged

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

Successfully merging a pull request may close this issue.

2 participants