Carapacik / swagger_parser

Dart package that takes an OpenApi definition file and generates REST clients based on retrofit and data classes for your project.
https://pub.dev/packages/swagger_parser
MIT License
90 stars 38 forks source link

Json request body should add appropriate content-type header #104

Closed paulopicasso closed 10 months ago

paulopicasso commented 10 months ago

Hello! I noticed that despite the following part in the swagger json:

"requestBody":{
    "content":{
      "application/json":{
        "schema":{
          "$ref":"#/components/schemas/RequestBodyObject"
        }
      }
    }
  }

a content-type header with application/json value is not added to the endpoint which causes issues in my app.

Could this be added?

Carapacik commented 10 months ago

By default retrofit has content type application/json But dio from version 5 remove this by default. Try to add to your dio client:

final dioClient = Dio(
    BaseOptions(
      contentType: Headers.jsonContentType, // <- this line
    ),
  );
paulopicasso commented 10 months ago

Yes that seems to work. Thanks!

elenaferr0 commented 10 months ago

Hi @Carapacik, I'm having a related issue. I have some PATCH endpoints which consume the content type application/json-patch+json. All other endpoints accept the standard application/json, so I cannot globally set the content type for Dio. I was wondering, should maybe the code generation also check if a specific content-type is required by the swagger and annotate the retrofit method with @Header? Something like this:

  @Header('Content-Type: application/json-patch+json')
  @PATCH('/users')
  Future<User> patchUser({
    @Body() required JsonPatch patch,
  });
Carapacik commented 10 months ago

I was wondering, should maybe the code generation also check if a specific content-type is required by the swagger and annotate the retrofit method with @Header?

Maybe something like this

  @Headers(<String, String>{'Content-Type': 'application/json-patch+json'})
  @PATCH('/users')
  Future<User> patchUser({
    @Body() required JsonPatch patch,
  });

I think it's worth considering an implementation when application/json is missing.