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
97 stars 45 forks source link

multipart/form-data with $ref #154

Closed bluemon82 closed 7 months ago

bluemon82 commented 8 months ago

When I define a multipart/form-data $ref the generated code fails to compile:

openapi: 3.0.2
info:
  title: API
  version: 1.0.0
paths:
  /api/item:
    post:
      summary: create  item
      requestBody:
        content:
          multipart/form-data:
            schema:
              $ref: '#/components/schemas/Item'
      responses:
        '200':
          description: dummy
          content:
            text/plain:
              schema:
                type: string
components:
  schemas:
    Item:
      type: object
      properties:
        images:
          type: array
          items:
            description: Binary image
            format: binary
            type: string

When I define it inline it works, the generated code compiles. Both variants seams to be valid in OpenApi.

openapi: 3.0.2
info:
  title: API
  version: 1.0.0
paths:
  /api/item:
    post:
      summary: create  item
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                images:
                  type: array
                  items:
                    description: Binary image
                    format: binary
                    type: string

      responses:
        '200':
          description: dummy
          content:
            text/plain:
              schema:
                type: string
StarProxima commented 8 months ago

@simonmoser-bluesource Hi, can you provide the generation results on the data and the full expected result?

I will look into this problem shortly.

bluemon82 commented 8 months ago

@StarProxima sure 😊

I think the Item Schema should inlined like this:

@RestApi()
abstract class ClientClient {
  factory ClientClient(Dio dio, {String? baseUrl}) = _ClientClient;

  /// create  item
  @MultiPart()
  @POST('/api/item')
  Future<String> postApiItem({
    @Part(name: 'images') List<File>? images,
  });
}

The tricky thing is what to do with Item? Should it not be generated as @JsonSerializable() , because in this case it fails with this message: Could not generate fromJson code for images because of type File.

StarProxima commented 7 months ago

@simonmoser-bluesource Hi, can you verify the correctness of the generation with your examples?

bluemon82 commented 7 months ago

@StarProxima Sorry for the late response. I just testet it with 1.15.4 and the generated code looks correct with my example! Thanks a lot for your great work. ❤️