Skip to content

204 returning empty body throw #252

Closed

Description

@kant2002

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
  • OpenApiClientProvider

Related information

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

Activity

  1. sergey-tihon commented on Jan 6, 2025

    @sergey-tihon
    Member

    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?

  2. kant2002 commented on Jan 7, 2025

    @kant2002
    Author

    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
    
  3. kant2002 commented on Jan 7, 2025

    @kant2002
    Author

    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'.
    
  4. sergey-tihon commented on Jan 7, 2025

    @sergey-tihon
    Member

    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.

  5. kant2002 commented on Jan 7, 2025

    @kant2002
    Author

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

  6. Numpsy commented on Mar 11, 2026

    @Numpsy
    Contributor

    Q. What should happen if you have an endpoint that can return either 200 or 204 under different circumstances?

    e.g if you have an OpenApi spec like

    "/api/MultipleResponseTypes": {
          "get": {
            "tags": [
              "MultipleResponseTypes"
            ],
            "responses": {
              "200": {
                "description": "OK",
                "content": {
                  "application/json": {
                    "schema": {
                      "type": "string"
                    }
                  }
                }
              },
              "204": {
                "description": "No Content"
              }
            }
          }
        }
    

    It currently appears to work when the server returns 200, but throws a json exception for the 204 case (where the response is empty so there is no json to parse).
    I'm not sure what it can do other than make the return type nullable or an option or something like that and return None/null for the 204 case, but I don't think trying to deserialize the json in a 204 case is right?

    I've seen similar issues in other client generators (e.g. RicoSuter/NSwag#4853) though maybe some of those throw an exception that contains the status code rather than a json error, so at least that can be special cased even if it's not an ideal approach)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions