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

Go: Broken Generated Code when Body is an Array of Scalar Values #4380

Closed
lampajr opened this issue Mar 21, 2024 · 2 comments · Fixed by #4388
Closed

Go: Broken Generated Code when Body is an Array of Scalar Values #4380

lampajr opened this issue Mar 21, 2024 · 2 comments · Fixed by #4388
Labels
Go help wanted Issue caused by core project dependency modules or library type:bug A broken experience
Milestone

Comments

@lampajr
Copy link
Contributor

lampajr commented Mar 21, 2024

Hey team 👋

Hope this is the right place to open Golang generation issues, today I faced a weird issue that I will try to describe below.

It seems that the generator produces a broken Golang code when trying to parse a body made by an array of scalars.

paths:
  /posts:
    post:
      description: 'Create post'
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: array
              items:
                format: int32
                type: integer
      responses:
        '201':
          description: Created
          content:
            application/json:
              schema:
                type: array
                items:
                  format: int32
                  type: integer

The generated ToPostRequestInformation is something like:

// ToPostRequestInformation create post
// returns a *RequestInformation when successful
func (m *PostsRequestBuilder) ToPostRequestInformation(ctx context.Context, body []int32, requestConfiguration *PostsRequestBuilderPostRequestConfiguration)(*i2ae4187f7daee263371cb1c977df639813ab50ffa529013b7437480d1ec0158f.RequestInformation, error) {
    requestInfo := i2ae4187f7daee263371cb1c977df639813ab50ffa529013b7437480d1ec0158f.NewRequestInformationWithMethodAndUrlTemplateAndPathParameters(i2ae4187f7daee263371cb1c977df639813ab50ffa529013b7437480d1ec0158f.POST, "{+baseurl}/posts", m.BaseRequestBuilder.PathParameters)
    if requestConfiguration != nil {
        requestInfo.Headers.AddAll(requestConfiguration.Headers)
        requestInfo.AddRequestOptions(requestConfiguration.Options)
    }
    requestInfo.Headers.TryAdd("Accept", "application/json")
    requestInfo.SetContentFromScalarCollection(ctx, m.BaseRequestBuilder.RequestAdapter, "application/json", body)
    return requestInfo, nil
}

The issue is that requestInfo.SetContentFromScalarCollection(ctx, m.BaseRequestBuilder.RequestAdapter, "application/json", body) cannot receive body (which is a []int32) as it is expecting a []interface{} type which is not allowed [1].

image

Maybe I missed something or I am doing something wrong (in that case, please let me know) but I think that the openapi is a valid one, therefore I am expecting a valid generated code.

Version

kiota --version                                                                                                                                                                              [17:54:51]
1.12.0+1735a2ff916ebec661974d9560f1dd00d3dc349b
go version                                                                                                                                                                                   [17:55:22]
go version go1.21.8 linux/amd64

How to reproduce the issue

To reproduce the issue I took the posts quickstart from the doc and I simply modified it to have a body made by a list of scalars.

  1. Clone my sample repo: https://github.com/lampajr/kiota_posts
  2. Run kiota generate -l go -c PostsClient -n kiota_posts/client -d ./posts-api.yaml -o ./client

[1] https://go.dev/wiki/InterfaceSlice

@baywet
Copy link
Member

baywet commented Mar 21, 2024

Hi @lampajr
Thanks for using kiota and for reporting this.
It's probably a regression, the code to concert the slice should be generated here.

writer.WriteLine($"{RequestInfoVarName}.SetContentFromScalar{collectionSuffix}({contextParameterName}, m.{requestAdapterPropertyName}, \"{sanitizedRequestBodyContentType}\", {bodyParamReference})");

And you can duplicate this unit test

public async Task WritesRequestExecutorForScalarTypes()

Is this something you'd be willing to submit a pull request for?

@baywet baywet added type:bug A broken experience help wanted Issue caused by core project dependency modules or library Go labels Mar 21, 2024
@baywet baywet added this to the Kiota v1.14 milestone Mar 21, 2024
@lampajr
Copy link
Contributor Author

lampajr commented Mar 21, 2024

Hey @baywet ,

Sure, I am not that expert but I can give it a try!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Go help wanted Issue caused by core project dependency modules or library type:bug A broken experience
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants