Skip to content

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

Closed

Description

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

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

Metadata

Assignees

No one assigned

    Labels

    Gohelp wantedIssue caused by core project dependency modules or librarytype:bugA broken experience

    Type

    No type

    Projects

    • Status

      Done ✔️

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions