christianhelle / refitter

A tool for generating Refit interfaces and contracts from OpenAPI specifications
https://refitter.github.io
MIT License
176 stars 38 forks source link

AnyType + BodySerializationMethod #423

Open MeikelLP opened 1 month ago

MeikelLP commented 1 month ago

Is your feature request related to a problem? Please describe. I want to send and receive AnyType as System.Text.Json.JsonElement for max convinience

Describe the solution you'd like Add a property alongside anyType like anyTypeBodySerializationMethod and let us adjust how the body is send.

Describe alternatives you've considered Writing the interface myself :(

Additional context System.Text.Json.JsonElement is send as a stream. This might work in some cases but most of the time we just wanna send it as string which is only possible with BodySerializationMethod.Json from what I can tell

Maybe my assumption is wrong - I'm new to Refit

christianhelle commented 1 month ago

@MeikelLP thanks for taking the time to suggest this

If you know what the Refit interface should look like in such a scenario, then it would be easier for me to add support. If what you need, has to be done on the contracts/models/dto then Refitter is limited by what NSwag can do for generating contracts/models/dto

MeikelLP commented 1 month ago

@MeikelLP thanks for taking the time to suggest this

If you know what the Refit interface should look like in such a scenario, then it would be easier for me to add support. If what you need, has to be done on the contracts/models/dto then Refitter is limited by what NSwag can do for generating contracts/models/dto

Thanks for the quick response!

Currently the interface method is generated as

Task<IApiResponse<System.Text.Json.JsonElement>> CreateMessageAsync([Body] System.Text.Json.JsonElement body, CancellationToken cancellationToken = default);

however I'd like it to be like this:

Task<IApiResponse<System.Text.Json.JsonElement>> CreateMessageAsync([Body(BodySerializationMethod.Serialized)] System.Text.Json.JsonElement body, CancellationToken cancellationToken = default);

This will cause the body to be serialized as json and send as StringContent rather than a StreamContent if I assume correctly.

christianhelle commented 1 month ago

@MeikelLP I have never used BodySerializationMethod.Json so I'm not sure what it does. But I can try it out and set some sort of HttpClient delegating handler to look at what payload type is on the HttpRequestMessage

Would you mind providing an example OpenAPI spec I can use to try things out?

MeikelLP commented 1 month ago

I'm currently working with a closed source proprietary API thus I cannot share the original schema. However I tried to mask everything unnecessary:

{
  "openapi": "3.0.0",
  "info": {
    "title": "Example",
    "version": "1.0.0"
  },
  "paths": {
    "/some/endpoint": {
      "post": {
        "summary": "Example",
        "operationId": "someThing",
        "requestBody": {
          "required": true,
          "content": {
            "application/ld+json": {
              "schema": {
                "type": "object",
                "additionalProperties": true
              }
            }
          }
        }
      }
    }
  }
}