Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way to specify the generated names #56

Open
las3r opened this issue Jun 7, 2021 · 6 comments
Open

Provide a way to specify the generated names #56

las3r opened this issue Jun 7, 2021 · 6 comments

Comments

@las3r
Copy link

las3r commented Jun 7, 2021

Love the generator, it is speeding up my workflow like crazy, so first off - thanks for that.

I'm using a .NET Core api, that provides me with nice OpenApi spec docs, that work perfectly with the codegenerator, however:

The names it generates for types, args, and endpoints is a bit verbose.

I've got an api defined as following:

{
 "/api/v1/Organization/{organizationId}/Departments": {
      "get": {
        "tags": [
          "Organization"
        ],
        "parameters": [
          {
            "name": "organizationId",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer",
              "format": "int32"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "$ref": "#/components/schemas/DepartmentInfoDto"
                  }
                }
              }
            }
          },
          "401": {
            "description": "Unauthorized",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ProblemDetails"
                }
              }
            }
          }
        },
        "security": [
          {
            "Azure_Active_Directory_B2C": [
              "https://xxxx.onmicrosoft.com/yyyy/zzzz"
            ]
          }
        ]
      }
    },
}

This resolves to:

Hook: useGetApiV1OrganizationByOrganizationIdDepartmentsQuery
Endpoint:

getApiV1OrganizationByOrganizationIdDepartments: build.query<
      GetApiV1OrganizationByOrganizationIdDepartmentsApiResponse,
      GetApiV1OrganizationByOrganizationIdDepartmentsApiArg
    >({
      query: (queryArg) => ({ url: `/api/v1/Organization/${queryArg.organizationId}/Departments` }),
    })

As you can see these names are bit verbose, and I'd like more control over them without having to change my Api endpoints. Perhaps it's possible to override part of the generated name? I like the prefix/suffix options, but those won't do (unless you want an even longer string).

I'd like to be able to either provide a custom function for naming. In this case I'd like to be able to transform getApiV1OrganizationByOrganizationIdDepartments into getOrganizationDepartments

@StefanBRas
Copy link
Contributor

I'm having a similar problem for a FastApi generated OpenApi schema.

@phryneas
Copy link
Collaborator

Hmm. I would think about it two ways:

  • either allow the schema to contain metadata on how an endpoint should be named. This would be cleaner but probably not everyone would have access to that
  • create some kind of endpointNameMapping.json file that looks like
{
  "getApiV1OrganizationByOrganizationIdDepartments": "getOrganization",
  "getApiV2OrganizationByOrganizationIdDepartments": false
}

Where the false would cause endpoints to be skipped in generation

@StefanBRas
Copy link
Contributor

@phryneas I would prefer option two her - preferably integrated in a configuration file.

@las3r
Copy link
Author

las3r commented Jun 13, 2021

Hmm. I would think about it two ways:

  • either allow the schema to contain metadata on how an endpoint should be named. This would be cleaner but probably not everyone would have access to that
  • create some kind of endpointNameMapping.json file that looks like
{
  "getApiV1OrganizationByOrganizationIdDepartments": "getOrganization",
  "getApiV2OrganizationByOrganizationIdDepartments": false
}

Where the false would cause endpoints to be skipped in generation

I would also opt for the second option. That would really help as right now I'm basically re-exporting them myself with pretty names, but the 'old' hook-names are still available! The first option won't do, as many times we're forced to consume a 3rd party api, so we're unable to change those api's

@pomali
Copy link

pomali commented Jun 14, 2021

Isn't "first" option already supported with operationId?

(uses getOperationName from oazapfts)
https://github.com/cellular/oazapfts/blob/master/src/codegen/generate.ts#L49

@Evgenyx82
Copy link

Same here.

My OpenApi3.json about 15k rows, and i got the result:

``json

delete15: build.mutation<Delete15ApiResponse, Delete15ApiArg>({
  query: (queryArg) => ({
    url: `/deviceparam/${queryArg.id}`,
    method: "DELETE",
  }),
}),
getAll5: build.query<GetAll5ApiResponse, GetAll5ApiArg>({
  query: () => ({ url: `/tsoperations` }),
}),
create25: build.mutation<Create25ApiResponse, Create25ApiArg>({
  query: (queryArg) => ({
    url: `/tsoperations`,
    method: "POST",
    body: queryArg.tsOperation,
  }),
}),
setDelete: build.mutation<SetDeleteApiResponse, SetDeleteApiArg>({
  query: (queryArg) => ({
    url: `/datasource/delete/${queryArg.id}/${queryArg.state}`,
    method: "PUT",
  }),
}),
getById11: build.query<GetById11ApiResponse, GetById11ApiArg>({
  query: (queryArg) => ({
    url: `/tsvalue/time/${queryArg.meastime}/meta/${queryArg.meta}`,
  }),
}),
delete6: build.mutation<Delete6ApiResponse, Delete6ApiArg>({
  query: (queryArg) => ({
    url: `/tsvalue/time/${queryArg.meastime}/meta/${queryArg.meta}`,
    method: "DELETE",
  }),
}),
create8: build.mutation<Create8ApiResponse, Create8ApiArg>({
  query: (queryArg) => ({
    url: `/entitytype`,
    method: "POST",
    body: queryArg.vocEntityType,
  }),
}),

``

i can't use this ;(

--
second problem

if PathVariable does not match it throw an error, but not really informative

``json

"/auth/keycloak/{role}/users1": {
  "get": {
    "tags": ["security-rest-controller"],
    "operationId": "roleUsers1",
    "parameters": [
      {
        "name": "roleName",
        "in": "path",
        "required": true,
        "schema": { "type": "string" }
      }
    ],
    "responses": {
      "200": {
        "description": "default response",
        "content": {
          "application/json": { "schema": { "type": "object" } }
        }
      }
    }
  }
},

``
if {role} does not match "name": "roleName", got the error

Error: path parameter role does not seem to be defined?

ofc i have found ) that type on 10k row... but in manual mode ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants