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
94 stars 43 forks source link

Incorrect method name generation #83

Closed StarProxima closed 12 months ago

StarProxima commented 12 months ago

Hi, thanks for developing a great package, I am getting some errors when generating.

When generating method names in the rest client, a summary is used, which may not be valid for the name.

Result:

import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';

import '../shared_models/email_form.dart';
import '../shared_models/verify_email_response.dart';

part 'email_client.g.dart';

@RestApi()
abstract class EmailClient {
  factory EmailClient(Dio dio, {String baseUrl}) = _EmailClient;

  @POST('/auth/email/verify')
  Future<VerifyEmailResponse> проверитьEmailНаСуществованиеAuthEmailVerifyPost({  // <- error
    @Body() required EmailForm body,
  });
}

Expected result:

import 'package:dio/dio.dart';
import 'package:retrofit/retrofit.dart';

import '../shared_models/email_form.dart';
import '../shared_models/verify_email_response.dart';

part 'email_client.g.dart';

@RestApi()
abstract class EmailClient {
  factory EmailClient(Dio dio, {String baseUrl}) = _EmailClient;

  /// Проверить Email на существование
  @POST('/auth/email/verify')
  Future<VerifyEmailResponse> authEmailVerifyPost({
    @Body() required EmailForm body,
  });
}

OpenApi:

{
  "openapi": "3.1.0",
  "info": {
    "title": "Microservice Auth API methods",
    "version": "0.2.2"
  },
  "paths": {
    "/auth/email/verify": {
      "post": {
        "tags": [
          "Email"
        ],
        "summary": "Проверить Email На Существование",
        "description": "Вернет True, если пользователь с указанным Email уже зарегистрирован, иначе False.",
        "operationId": "Проверить_Email_на_существование_auth_email_verify_post",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/EmailForm"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/VerifyEmailResponse"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "EmailForm": {
        "properties": {
          "email": {
            "type": "string",
            "format": "email",
            "title": "Email",
            "description": "Адрес электронной почты"
          }
        },
        "type": "object",
        "required": [
          "email"
        ],
        "title": "EmailForm"
      },
      "VerifyEmailResponse": {
        "properties": {
          "exists": {
            "type": "boolean",
            "title": "Exists",
            "description": "Возвращает true, если сущность существует, иначе false"
          }
        },
        "type": "object",
        "required": [
          "exists"
        ],
        "title": "VerifyEmailResponse"
      }
    }
  },
  "tags": [
    {
      "name": "Auth",
      "description": "Auth Management"
    }
  ]
}
StarProxima commented 12 months ago

@Carapacik What do you think about the possibility of adding a flag to the swagger_parser configuration that leaves only path in the method name, and outputs summary as a documentation comment?

With the current implementation there are no errors, but there may be difficulties in interpreting method names as only some words may be used.

If you want I could look into implementing this feature.