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

Exception while getting simple string from Web API #196

Closed
1 task done
francotiveron opened this issue Jul 2, 2022 · 2 comments · Fixed by #202
Closed
1 task done

Exception while getting simple string from Web API #196

francotiveron opened this issue Jul 2, 2022 · 2 comments · Fixed by #202

Comments

@francotiveron
Copy link

francotiveron commented Jul 2, 2022

Description

Failure when calling a simple string returning GET call

Repro steps

  • create a F# web api app with the following controller
[<ApiController>]
type YatpApiController() = 
   inherit ControllerBase()

   [<HttpGet("version")>]
   member __.Get() = "0.0"
  • create a F# client console application
module Yatp.Client

open SwaggerProvider
open System.Net.Http
open System

//let [<Literal>] Schema = "swagger.json"
let [<Literal>] Schema = @"http://localhost:5166/swagger/v1/swagger.json"
type YatpClient = OpenApiClientProvider<Schema>
let client = YatpClient.Client(new HttpClient(BaseAddress = Uri(@"http://localhost:5166/")))
let version = client.GetVersion().Result //Error
printfn "%A" version

Schema

{
  "openapi": "3.0.1",
  "info": {
    "title": "Yatp.Api",
    "version": "1.0"
  },
  "paths": {
    "/version": {
      "get": {
        "tags": [
          "YatpApi"
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "text/plain": {
                "schema": {
                  "type": "string"
                }
              },
              "application/json": {
                "schema": {
                  "type": "string"
                }
              },
              "text/json": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    },

Expected behavior

the value "0.0" should be returned and then printed

Actual behavior

An exception is raised in the client.GetVersion().Result line

One or more errors occurred. (The JSON value could not be converted to System.String. Path: $ | LineNumber: 0 | BytePositionInLine: 3.)

Known workarounds

None

Affected Type Providers

  • OpenApiClientProvider

Related information

  • Operating system: Windows 10
  • .NET Runtime: .NET 6

NOTE: C# client generated with NSwag or Visual Studio works as expected

var yatpClient = new Yatp.Client.YatpClient(@"http://localhost:5166/", new HttpClient());
var version = await yatpClient.VersionAsync(); //OK
Console.WriteLine(version);
@sergey-tihon
Copy link
Member

Workaround here will be to modify server to return JSON "0.0" instead of plain text 0.0

It can be done using Produces(MediaTypes.ApplicationJson) attribute

[<ApiController>]
type YatpApiController() = 
   inherit ControllerBase()

   [<HttpGet("version"); Produces(MediaTypes.ApplicationJson)>]
   member __.Get() = "0.0"

another option here is to inherit from provided client and override member Deserialize, basically do nothing with response instead of default behavior JsonSerializer.Deserialize(value, retTy, options)

sergey-tihon added a commit that referenced this issue Aug 15, 2022
sergey-tihon added a commit that referenced this issue Aug 15, 2022
* feat: add tests that repro #196

* feat: add Accept header
@sergey-tihon
Copy link
Member

Fixed in v2.0.0-beta3 by adding Accept header to requests, forcing server to respond with json.

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

Successfully merging a pull request may close this issue.

2 participants