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

Flag to use the path for the generated method name #87

Closed StarProxima closed 11 months ago

StarProxima commented 11 months ago

This feature request is related to https://github.com/Carapacik/swagger_parser/issues/83.

Proposal

Add a flag to the swagger_parser configuration that leaves only path in the method name.

swagger_parser:
  path_for_method_name: false # Optional. Set to 'true' to use only the endpoint path for the method name. If set to false, the operationId will be used. Default: false

Motivation

Sometimes the summary in OpenAPI can be irrelevant (invalid, too long or short, in a different language), which can be confusing when searching for the right method in the api client.

The optional flag will leave only path in the name, which will allow better navigation through the code.

Suggested behavior

Current method generation:

  @POST('/auth/email/verify')
  Future<void> emailUsersDbAuthEmailVerifyPost({
    @Body() required Object body,
  });

Proposed method generation:

  @POST('/auth/email/verify')
  Future<void> authEmailVerifyPost({
    @Body() required Object 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 на cуществование в users_db",
        "description": "Вернет True, если пользователь с указанным Email уже зарегистрирован, иначе False.",
        "operationId": "Проверить_Email_на_существование_в_users_db_auth_email_verify_post",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object"
              }
            }
          },
          "required": true
        },
        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "type": "object"
              }
            }
          }
        }
      }
    }
  }
}
Carapacik commented 11 months ago

I think you can do something similar optional_id_for_method_name As you have only the opposite.

StarProxima commented 11 months ago

I think you can do something similar optional_id_for_method_name As you have only the opposite.

How exactly should the flag with the word 'optional' work?

Specifically in my case the operationId is composed as summary + path. It is not very clear what id means in this case.

I'm not sure that the optional_id_for_method_name wording will make it clear to users how the flag works.

Maybe something like only_path_for_method_name?

If I'm misunderstanding, can you tell me in more detail what you mean?

Carapacik commented 11 months ago

Something like this code

https://github.com/Carapacik/swagger_parser/blob/2c3a7dfa57cc41ccf6181400ad5c51e6f188e01c/swagger_parser/lib/src/parser/parser.dart#L406

if (thisBoolVariable) {
  requestName = (key + path).toCamel
}
StarProxima commented 11 months ago

Okay, got it. I'll try to implement it this week.

What's the final name and description for the flag?

Carapacik commented 11 months ago

path_method_name : false