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

Form Data Option Types not serialized properly #214

Open
1 of 2 tasks
rametta opened this issue Dec 15, 2022 · 2 comments
Open
1 of 2 tasks

Form Data Option Types not serialized properly #214

rametta opened this issue Dec 15, 2022 · 2 comments
Milestone

Comments

@rametta
Copy link

rametta commented Dec 15, 2022

Description

When calling a request that specifies FormData as the type and has fields that can be Option types, when specifying a value like Some true or Some 6 it is actually sending the whole option as a string to the API like Some(true) and Some(6) instead of just true or 6.

Example

let formData = MyProvider.OperationTypes.UpdateItem_formData()
formData.Name <- "My name"
formData.SomeFieldThatExpectsABool <- Some true
formData.SomeFieldThatExpectsANumber <- Some 6
let! res = client.UpdateItem(formData)

Intrestingly, it works as expected when specifying the Provider like this OpenApiClientProvider<Schema, PreferNullable=true> (with PreferNullable) but then I have to use Nullable true or Nullable 6 and it'll send to API as expected, but with Option types it does not.

This is on version 2.0.0-beta6

Repro steps

  1. Have a provider that needs a form data request and field that exposes as an Option
  2. Set the value of the Option to something like Some 6 or whatever the option type needs
  3. See the backend receive the value as Some(6) instead of just 6

Expected behavior

The option type value should be unwrapped before sending to API if it's Some and should be null if None

Actual behavior

The option type value is not unwrapped before sending to API

Known workarounds

Use PreferNullable=true instead, that works

Affected Type Providers

  • SwaggerClientProvider
  • OpenApiClientProvider (I only tested with this one)

Related information

  • Operating system Mac OS Montery
  • Branch
  • .NET Runtime, CoreCLR or Mono Version net6.0
  • Performance information, links to performance testing scripts
@sergey-tihon sergey-tihon added this to the v2.0.0 milestone Dec 22, 2022
@sergey-tihon
Copy link
Member

@rametta would you like to contribute the fix for this?

I believe that it should be relatively simple, like handling Options here https://github.com/fsprojects/SwaggerProvider/blob/master/src/SwaggerProvider.Runtime/RuntimeHelpers.fs#L165

TP actually emits code in the design time that call this method with the object that should be transformed into formData
https://github.com/fsprojects/SwaggerProvider/blob/master/src/SwaggerProvider.DesignTime/v3/OperationCompiler.fs#L340-L346

An Ideal way to contribute

  1. add controller/attribute to test API that reproduces formData with optional params - most likely here https://github.com/fsprojects/SwaggerProvider/blob/master/tests/Swashbuckle.WebApi.Server/Controllers/SpecialCasesControllers.fs
  2. write a test that calls this method using the current TP code - here https://github.com/fsprojects/SwaggerProvider/blob/master/tests/SwaggerProvider.ProviderTests/v3/Swashbuckle.SpecialCasesControllers.Tests.fs
  3. commit and open a pull request to ensure that CI fails as well
  4. create a fix, commit, merge, release, happiness 😁

@rametta
Copy link
Author

rametta commented Dec 29, 2022

@sergey-tihon thanks! I'll give it a shot when I'm back from vacation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants