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] Pass by value/Pass by Reference changes breaking Go client #11467

Closed
6 tasks done
meghana-rajashekar opened this issue Jan 31, 2022 · 0 comments
Closed
6 tasks done

Comments

@meghana-rajashekar
Copy link

meghana-rajashekar commented Jan 31, 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?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The commit in the PR: #8491 which resolves the pointer issue missed the logic to perform similar operation in case of Unmarshalling of the JSON.
Link to commit: aeneasr@0c5e5ae
Due to this, there is a mismatch between the data type for BreedList property(This issue is seen for array of object type property) between Dog type and DogWithoutEmbeddedStruct type(Present inside UnmarshalJSON() method).
Following is the model schema used to reproduce the issue.

Snippet of spec file
schemas:
    Dog:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - type: object
          properties:
            legs:
               $ref: '#/components/schemas/Legs'
            breedList:
               type: array
               items:
                   description: A dimension value matching search query
                   type: object
    Cat:
      allOf:
        - $ref: '#/components/schemas/Animal'
        - type: object
          properties:
            declawed:
              type: boolean
            legs:
               $ref: '#/components/schemas/Legs'
    Legs:
        type: object
        required:
        - legs
        properties:
            legs:
              enum:
              - '2'
              - '4'
              default: '4'
              x-enum-as-string: true
            name:
               type: string
    Animal:
      type: object
      discriminator:
        propertyName: className
      required:
        - className
      properties:
        className:
          type: string
        color:
          type: string
          default: red
        run:
           type: boolean
           default: True
           readOnly: true
        results:
            type: array
            items:
                type: object

The following Snippet of code is generated:
Definition for {{ classname }} struct generated at the beginning:

type Dog struct {
        Animal
        Legs *Legs `json:"legs,omitempty"`
        BreedList []map[string]interface{} `json:"breedList,omitempty"`
        AdditionalProperties map[string]interface{}
}

Definition of struct generated inside UnmarshalJSON() method:

type DogWithoutEmbeddedStruct struct {
        Legs *Legs `json:"legs,omitempty"`
        BreedList *[]map[string]interface{} `json:"breedList,omitempty"`
}

We can see that there is the difference between the type of BreedList inside Dog(which is a non-pointer type) and DogWithoutEmbeddedStruct type(Which is a pointer type).

Expected output:

No issues when the code is built.

Actual output:
go build -o myproject
./model_dog.go:149:20: cannot use varDogWithoutEmbeddedStruct.BreedList (type *[]map[string]interface {}) as type []map[string]interface {} in assignment
openapi-generator version

5.2.1

OpenAPI declaration file content or url

https://gist.githubusercontent.com/the-akhil-nair/ffd8a387e24e90b8759bc7a3f53fe0f0/raw/c69c490139a95351df5e03ff1a40e9099e0e0782/go_pbv_pbv_petstore_example.yaml

Generation Details
config.yaml file content:
# Golang Configuration
enumClassPrefix: true
useOneOfDiscriminatorLookup: true
# Enable additionalProperties by default
additionalProperties:
    disallowAdditionalPropertiesIfNotPresent: false

java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -i https://gist.githubusercontent.com/the-akhil-nair/ffd8a387e24e90b8759bc7a3f53fe0f0/raw/c69c490139a95351df5e03ff1a40e9099e0e0782/go_pbv_pbv_petstore_example.yaml -g go -o ../animal -c ../config.yaml

Steps to reproduce
  1. Pull the master branch and compile the openapi-generator code using mvn.
  2. Run generation cli commands as mentioned in the above steps.
  3. Change the directory to ../animal and run go build -o myproject
Related issues/PRs

#8491

Suggest a fix

The logic used to define the struct for classname should also be reflected when it's defined for WithoutEmbeddedStruct inside UnmarshalJSON() method.

PR raised for the solution:
#11466

@meghana-rajashekar meghana-rajashekar changed the title Pass by value/Pass by Reference changes breaking Go client [GO] Pass by value/Pass by Reference changes breaking Go client Jan 31, 2022
@meghana-rajashekar meghana-rajashekar changed the title [GO] Pass by value/Pass by Reference changes breaking Go client [BUG][GO] Pass by value/Pass by Reference changes breaking Go client Jan 31, 2022
@wing328 wing328 closed this as completed Mar 25, 2022
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