fsprojects / SwaggerProvider

F# generative Type Provider for Swagger

Home Page:https://fsprojects.github.io/SwaggerProvider/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Form Data Option Types not serialized properly

rametta opened this issue · comments

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

@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 😁

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