SwaggerProvider icon indicating copy to clipboard operation
SwaggerProvider copied to clipboard

204 returning empty body throw

Open kant2002 opened this issue 1 year ago • 5 comments

Description

I have API method which described like this

  "paths": {
    "/auth": {
      "put": {
        "operationId": "CreateAuth",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "body",
            "in": "body",
            "schema": {
              "$ref": "#/definitions/CreateAuth"
            }
          }
        ],
        "responses": {
          "204": {
            "description": "No Content"
          }
        },
        "deprecated": false
      }
....

this breaks when I attempt to call this endpoint with following exception

System.AggregateException: One or more errors occurred. (The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.)
 ---> System.Text.Json.JsonException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
 ---> System.Text.Json.JsonReaderException: The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.

Known workarounds

I did not find any workarounds for this.

Affected Type Providers

  • [ ] SwaggerClientProvider
  • [x] OpenApiClientProvider

Related information

  • Operating system
  • Branch
  • .NET Runtime, CoreCLR or Mono Version
  • Performance information, links to performance testing scripts

kant2002 avatar Dec 28 '24 19:12 kant2002

I think the bug somewhere here https://github.com/fsprojects/SwaggerProvider/blob/master/src/SwaggerProvider.DesignTime/v3/OperationCompiler.fs#L156-L168

we see that API produces json and infers some response type and then later try to parse json from empty response body.

Would you like to conduct test for this use case?

  • add new endpoint here https://github.com/fsprojects/SwaggerProvider/tree/master/tests/Swashbuckle.WebApi.Server/Controllers
  • add code that call this endpoint here https://github.com/fsprojects/SwaggerProvider/tree/master/tests/SwaggerProvider.ProviderTests/v3

sergey-tihon avatar Jan 06 '25 09:01 sergey-tihon

I clone repo and after running build.cmd receive following error

  SwaggerProvider.Runtime failed with 1 error(s) (17.0s)
    C:\SwaggerProvider\paket-files\fsprojects\FSharp.TypeProviders.SDK\src\ProvidedTypes.fs(3224,7): error FS0842: This attribute is not valid for use on this language element

This is around

[<AutoOpen>]

[<Struct>]
type ILTableName(idx: int) =
    member __.Index = idx
    static member FromIndex n = ILTableName n

kant2002 avatar Jan 07 '25 02:01 kant2002

Okay. I take a deeper look, and seems to be issue arise in following way

type WebAPI2 = OpenApiClientProvider<"http://localhost:5000/swagger/v1/swagger.json", IgnoreOperationId=true>

Notice that I pass Swagger document to OpenApiClient. I cannot make test case for this since there other errors when compiling Swagger schema

    C:\SwaggerProvider\tests\SwaggerProvider.ProviderTests\v3\Swashbuckle.ReturnControllers.Tests.fs(17,16): error FS3033: The type provider 'SwaggerProvider.OpenApiClientTypeProvider' reported an error: Cannot compile object 'GetApiUpdateEnum_x' when schema is 'null'
    C:\SwaggerProvider\tests\SwaggerProvider.ProviderTests\v3\Swashbuckle.ReturnControllers.Tests.fs(17,16): error FS3033: The type provider 'SwaggerProvider.OpenApiClientTypeProvider' reported an error: Cannot compile object 'GetApiUpdateEnum_x' when schema is 'null'
    C:\SwaggerProvider\tests\SwaggerProvider.ProviderTests\v3\Swashbuckle.ReturnControllers.Tests.fs(33,13): error FS0039: The type 'WebAPI2' does not define the field, constructor or member 'Client'.
    C:\SwaggerProvider\tests\SwaggerProvider.ProviderTests\v3\Swashbuckle.NoContentControllers.Tests.fs(16,21): error FS0039: The type 'Object' does not define the field, constructor or member 'PostApiNoContent'.

kant2002 avatar Jan 07 '25 04:01 kant2002

error FS0842: This attribute is not valid for use on this language element

This is strange, The Windows CI build was ok yesterday https://github.com/fsprojects/SwaggerProvider/actions/runs/12639820322/job/35218945655

Notice that I pass Swagger document to OpenApiClient.

Why? I remember that swagger support by Microsoft.OpenApi.Readers was quite limited.

Yesterday I also added API for NoContent and tests. They works.

sergey-tihon avatar Jan 07 '25 07:01 sergey-tihon

Why? Because of my mistake :) anyway it was confusing experience.

kant2002 avatar Jan 07 '25 09:01 kant2002