swagger-api / swagger-play

Apache License 2.0
330 stars 181 forks source link

Unable to produce valid operation URLs with Play 2.5 #111

Open ShokuninSan opened 7 years ago

ShokuninSan commented 7 years ago

versions: playframework 2.5.9 swagger-play2 1.5.3

application.conf:

play.http.context = „foo“
swagger.api.basepath = „/foo/api“

routes (application context and api realm are both intentionally named „foo", so this is no mistake):

GET     /api/docs/foo/v1/docs/registrations                   @controllers.ApiHelpController.getResource(path = „/foo/v1/docs/registrations")
POST    /api/foo/v1/registrations/members                     @api.foo.registrations.v1.RegistrationsCtrl.registerMember
POST    /api/foo/v1/registrations/nonmembers                  @api.foo.registrations.v1.RegistrationsCtrl.registerNonMember

result: operation URL returned in spec is http://0.0.0.0/foo/api/api/foo/v1/registrations/members where I actually expect this: http://0.0.0.0/foo/api/foo/v1/registrations/members.

I think the reason ist that PlayReader.getPathFromRoute(PathPattern pathPattern, String basePath) should actually remove the basePath from the pathPattern in line 232, but it fails to do so, because the pathPattern does not include the play.http.context.

Is there a way to overcome this issue?

ShokuninSan commented 7 years ago

It could work when I set swagger.api.basepath in application.conf to /foo only, but in this case the implementation of PlayReader.getPathFromRoute removes the foo which must remain in the operationPath as it is not part of the basepath.

Thus instead of

@ val s = "/api/foo/v1/registrations/members" 
s: String = "/api/foo/v1/registrations/members"
@ s.replaceFirst("foo", "")

as it is done in PlayReader.java#L232 and which results in

res7: String = "/api//v1/registrations/members"

the method could do

@ s.replace("^foo", "") 
res8: String = "/api/foo/v1/registrations/members"

i.e. use replace and prefix the basePath parameter with a ^ to avoid removing substrings matching basePath unintenionally.

laszlokiraly commented 7 years ago

Any news on the issue? I found a dirty workaround by configuring swagger.api.basepath = "/foo/./" in application.conf.