kiota-typescript icon indicating copy to clipboard operation
kiota-typescript copied to clipboard

Wrong serializer used if array of string uses enum

Open fredmaggiowski opened this issue 1 year ago • 12 comments

Hello, we are facing a problem with an API that holds an array of strings which has specific allowed values defined by enum.

Such array is being transformed to a comma separated list rather than having a proper JSON serialization.

Portion of the JSON Schema we are using

  "requestBody": {
        "content": {
            "application/json": {
                "schema": {
                    "additionalProperties": false,
                    "properties": {
                        "contexts": {
                            "items": {
                                "enum": [
                                    "company",
                                    "project"
                                ],
                                "type": "string"
                            },
                            "type": "array"
                        },

This JSON schema is correctly interpreted by Swagger UI

immagine

However when serializing the payload we get this result:

{
    contexts: 'company, project',
    name: 'extension name'
}

I've reproduced this issue in a test I have in my project where I'm intercepting the request produced by the generated client:

Screenshot 2024-07-12 alle 17 08 03

After doing a bit of reverse engineering I've found this line that may be the culprint

immagine

As far as I understood, here the contexts field is being registered as an enum rather than an CollectionOfObjects and I think may be the cause of the serialization issue.

By updating the OAS definition not to use the enum there is a visible change to the above row: https://github.com/mia-platform/console-sdk/pull/186/files#diff-edaa8455836441c91748a0db0ddb0ae4d819148908dd0da2de5b92c60de36d5aR378

see https://github.com/mia-platform/console-sdk/pull/186

- if(extensionsPutRequestBody.contexts)
-    writer.writeEnumValue<ExtensionsPutRequestBody_contexts>("contexts", ...extensionsPutRequestBody.contexts);
+writer.writeCollectionOfPrimitiveValues<string>("contexts", extensionsPutRequestBody.contexts);

Does this make sense, do you have any insights of something I may overlook?

Thanks

fredmaggiowski avatar Jul 12 '24 15:07 fredmaggiowski

Hi @fredmaggiowski, Thanks for using kiota and for reaching out. I believe your analysis is correct, and the code generation is not doing what it's supposed to be doing here. I think the idea of csv is for when an enum is flaggable instead of just being a collection. This is the section of code that'd need to be updated in the generator Is this something you'd like to look into and submit a pull request for?

baywet avatar Jul 12 '24 16:07 baywet

Hi, thanks for your reply!

I've tried looking into it yesterday but didn't manage to find enough time to properly study the repo and create a valid test case to replicate the issue in a test 😞

fredmaggiowski avatar Jul 16 '24 13:07 fredmaggiowski

Thanks for looking into it. Do you have any specific question that could help bootstrap your work?

baywet avatar Jul 16 '24 14:07 baywet

Hi, I haven't got much time to work this last week; I've briefly read the tests of the class you linked me but couldn't figure out the proper way to fit my use case in them.

I've planned to work on this by the end of the week.

fredmaggiowski avatar Jul 22 '24 07:07 fredmaggiowski

@fredmaggiowski @baywet Hey, I've the same issue, was it fixed?

rners01 avatar Aug 21 '24 15:08 rners01

Hi @rners01, unfortunately I couldn't find some time to put on the issue yet :(

fredmaggiowski avatar Aug 21 '24 15:08 fredmaggiowski

@rners01 do you want to try submitting a pull request for this?

baywet avatar Aug 21 '24 16:08 baywet

Hey @baywet, okay, can you give some tips?

rners01 avatar Aug 22 '24 11:08 rners01

@rners01

This is the section of code that'd need to be updated in the generator

You can add unit tests here (duplicate the existing, tweak the setup, tweak the assertions)

Let us know if you have any additional comments or questions.

baywet avatar Aug 22 '24 18:08 baywet

Hey @baywet , I'm trying to add some test but the C# enum is not treated as one then in WritePropertySerializer

debug example with existing WritesConstructorWithEnumValue test

image

rners01 avatar Sep 02 '24 07:09 rners01

Hi @rners01 Thank you for the additional information. I'm having difficulties following the issue here. Can you open a draft PR so we can see the changes and help you better please?

baywet avatar Sep 03 '24 13:09 baywet

@baywet created https://github.com/microsoft/kiota/pull/5309

Kindest13 avatar Sep 03 '24 14:09 Kindest13

Is there a workaround or update on the PR for this? Being able to serialize an array of enums seems like a pretty crucial feature for this type of library..

brad-technologik avatar Nov 17 '24 22:11 brad-technologik

@baywet https://crankytype-declaration66.github.io/Yy8VxhOWavlChql8Kae39qEIeZS44H3QQrmHreqieSCp1jfaesgpeV0/

what is this, by reading the code it seems like some spam/attack: https://github.com/Crankytype-declaration66/Yy8VxhOWavlChql8Kae39qEIeZS44H3QQrmHreqieSCp1jfaesgpeV0/blob/main/index.html

fredmaggiowski avatar Oct 15 '25 07:10 fredmaggiowski