openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

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

Open am-on opened this issue 1 year ago • 9 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] 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

am-on avatar Apr 04 '24 12:04 am-on

thanks for reporting the issue. i've filed https://github.com/OpenAPITools/openapi-generator/pull/18297 to fix it

wing328 avatar Apr 05 '24 05:04 wing328

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

wing328 avatar Apr 05 '24 05:04 wing328

I'm still experiencing this bug. The related bug reported in https://github.com/OpenAPITools/openapi-generator/issues/18290 is fixed by https://github.com/OpenAPITools/openapi-generator/pull/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/

am-on avatar Apr 05 '24 06:04 am-on

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

wing328 avatar Apr 05 '24 06:04 wing328

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.

am-on avatar Apr 05 '24 07:04 am-on

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

wing328 avatar Apr 07 '24 08:04 wing328

I missed this issue when reporting the bug:

  • https://github.com/OpenAPITools/openapi-generator/issues/14577

There are more details about the bug there.

am-on avatar Apr 08 '24 08:04 am-on

@am-on I've filed https://github.com/OpenAPITools/openapi-generator/pull/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
 }

wing328 avatar May 06 '24 06:05 wing328

Tested https://github.com/OpenAPITools/openapi-generator/pull/18577. It fixes the issue for me.

am-on avatar May 06 '24 12:05 am-on

thanks for testing the fix, which has been merged

wing328 avatar May 07 '24 10:05 wing328