Closed wizofaus closed 4 years ago
@wizofaus
See in ISwaggerOptions.methodNameMode
.
methodNameMode is either operationId or path, how will that help? Thanks for the super quick reply, but I'm a bit confused why you closed this...
@wizofaus I think your mean that you need to generate unique method name? Maybe I didn't got it.
I mean that I need globally unique operationIds to comply with the swagger spec, e.g. getFoo, getBar, getWidget, updateFoo, updateBar, udpateWidget etc. But the resource name (Foo/Bar/Widget) doesn't need to be included in the method name because it's already in the name of the typescript class: FooService.get FooService.update BarService.get BarService.update WidgetService.get WidgetService.update
i.e. I'd like an option that when generating the methodName that takes the operationId then strips off the "resource name"/service name from the end.
I got it. I think you can put all the methods in a default Service, like that
class Service{
static [operationId](){}
}
We want to keep the generated client code as it is now - apart from the fact we have a substantial amount of code using it, it looks clean and is well-organized and easy to read. But the only way to achieve it is to violate the swagger 2.0/openapi spec and NOT have globally unique operationIds.
Emmm....., I am a little confused now. What do you want ,now. Codegen currently has two methods for generating method names.
// use operationId
[tagName]Service {
static [operationId](){}
}
// use path
[tagName]Service {
static [last-path](){}
}
Usually we recommend using operationId, because it is the specification of swagger2.0, and it can be customized according to requirements. And then, the tagName is just for grouping
I'm saying I'd like
[tagName]Service {
static [operationId-with-tagName-suffix-removed](){}
}
e.g.
"/v1/foo/{id}":{"get":{"tags":["Foo"],"operationId":"GetFoo"
...
"/v1/foo/{id}":{"post":{"tags":["Foo"],"operationId":"UpdateFoo"
...
"/v1/bar/{id}":{"get":{"tags":["Bar"],"operationId":"GetBar"
...
"/v1/bar/{id}":{"post":{"tags":["Bar"],"operationId":"UpdateBar"
...
should generate
FooService {
static Get(id){}
static Update(id, ...){}
}
BarService {
static Get(id){}
static Update(id, ...){}
}
I'm saying I'd like
[tagName]Service { static [operationId-with-tagName-suffix-removed](){} }
it is a bit unreasonable
As long as the only operations we have for specifying the methodName are via operationId or path, then there's no way of getting the client stubs we want and complying with the swagger 2.0 specification requiring globally unique operationId's.
I'm happy to put up a PR for it if you accept external contributions.
Okey, Welcome! But, it only remove methodName's suffix
I'll add a new methodNameMode which is operationIdWithoutTagName then?
Actually I went with shortOperationId, and it's basically just req.operationId.replace(req.className, '')
, though that also required adding className
to the IRequestMethods interface.
Unfortunately you're not using npm so I haven't quite figured out to compile locally, but hopefully it will work...
req.operationId.replace(req.className, '')
It is so bad. It should only remove at suffix. you can use trimString()
of utils.ts
Fair enough, tweaked a few things and put up a PR. I noticed none of your examples had methodNameMode: 'operationId'
to test against, not sure if you have a process for creating new examples.
new example by you.
I'm not sure what you're saying, you want me to add one before approving the PR?
Yes.
And then, codegen
has a method similar to 'trimSuffix'.
I looked at the functions in utils.ts but they were too complex and not particularly efficient for what I needed.
try it
Yep, I did, that's why I went with a simpler method that was suitable to what was needed. I've pushed a new commit with an example.
Thanks for merging, having trouble getting it to download the latest version, do you need to republish or something?
Currently codegen takes our swagger and uses the "tags" metadata to generate a typescript class for each resource, for which there are then your typical create/get/list/delete type methods. But in order to achieve this we have to violate the swagger 2 spec which requires globally-unique operationIds. What I'd like is the ability to have codegen strip the suffix off each operationId that matches the resource name (as per the "tags" metadata), so that our operationIds can be, e.g. getFoo, getBar, getWidget but the functions generated are FooService.get(), BarService.get(), WidgetService.get() etc., as per what we have now. Is this a reasonable request? I assume there's no "plugin" mechanism for generating function names? (NB using the 'path', the other alternative for methodNameMode, is not feasible).