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

Trim trailing '/' from basePathWithoutHost #967

Merged
merged 2 commits into from
Sep 20, 2018
Merged

Trim trailing '/' from basePathWithoutHost #967

merged 2 commits into from
Sep 20, 2018

Conversation

bjgill
Copy link
Contributor

@bjgill bjgill commented Sep 4, 2018

PR checklist

  • Read the contribution guidelines.
  • Ran the shell script under ./bin/ to update Petstore sample so that CIs can verify the change. (For instance, only need to run ./bin/{LANG}-petstore.sh and ./bin/security/{LANG}-petstore.sh if updating the {LANG} (e.g. php, ruby, python, etc) code generator or {LANG} client's mustache templates). Windows batch files can be found in .\bin\windows\.
  • Filed the PR against the correct branch: master, 3.3.x, 4.0.x. Default: master.
  • Copied the technical committee to review the pull request if your PR is targeting a particular programming language.

Description of the PR

basePath has its trailing / trimmed if present. This PR does the same thing for basePathWithoutHost as well. This fixes the common use of {{{basePathWithoutHost}}}{{{path}}} in the presence of trailing /.

This is now necessary as a result of #951, which causes basePathWithoutHost to otherwise default to /. This breaks rust-server (and probably other generators as well), which assumes that it can safely perform a string concatenation of basePathWithoutHost and path.

Given that the normal use is {{{basePathWithoutHost}}}{{{path}}}.
@jmini
Copy link
Member

jmini commented Sep 5, 2018

I did some experiments.


A lot of templates are concatenating basePathWithoutHost and path together like:

/**
* {{httpMethod}} {{operationId}}
* Summary: {{summary}}
* Notes: {{notes}}
{{#hasProduces}} * Output-Formats: [{{#produces}}{{{mediaType}}}{{#hasMore}}, {{/hasMore}}{{/produces}}]{{/hasProduces}}
*/
$app->{{httpMethod}}('{{{basePathWithoutHost}}}{{{path}}}', '{{classname}}@{{operationId}}');

See {{{basePathWithoutHost}}}{{{path}}} line 22.


This is not a problem, because in theory path must start with a /

From the spec (source):

The field name MUST begin with a slash.

Swagger-Editor online or the KaiZen-Editor are reporting errors in a case like this:

openapi: '3.0.1'
info:
  title: ping test
  version: '1.0'
servers:
  - url: 'http://localhost:8000/api/'
paths:
  ping/me:
    get:
      operationId: pingGet
      responses:
        '201':
          description: OK

But swagger-parser is not reporting any error, letting the user generate code with OpenAPI-Generator.


The example I have presented before, now generates wrong code (with this PR):

/**
 * get pingGet
 * Summary: 
 * Notes: 

 */
Route::get('/apiping/me', 'DefaultController@pingGet');

Should be /api/ping/me as it was before the change introduced by this PR.


In my opinion this PR is correct, and should be merged.

We need to add an additional validation (not just the one from swagger-parser) to prevent people having wrong Specification as input. We need to ensure that each path starts with /.

@jmini
Copy link
Member

jmini commented Sep 8, 2018

I have opened #997 to add the discussed additional validation.

@bjgill
Copy link
Contributor Author

bjgill commented Sep 13, 2018

FYI @OpenAPITools/generator-core-team

This PR is not targeted at any particular generator, but will fix rust-server and (likely) a bunch of others as well.

Any chance of getting this merged at some stage?

basePath = basePath.substring(0, basePath.length() - 1);
}

basePathWithoutHost = contextPath.replaceAll("/$", ""); // for backward compatibility
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjgill for contextPath, I think you will need to do the same by removing the trailing "/". I remember a few generators are still using contextPath in the templates

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjgill wouldn't this need to remove all trailing slashes, not just the last one?

For instance, although technically a valid URL, this would cause issues:

http://localhost:8000/api/////

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The purpose of this PR is to work around recent changes to swagger-parser that mean that the many generators that do {{{basePathWithoutHost}}}{{{path}}} stop introducing a '//' between the two in some cases.

As such, I think I agree that we could apply this fix to contextPath as well without much fuss. I'll add a commit to that effect shortly.

I'd be more nervous about extending this to remove all trailing slashes. Whilst very weird, this is technically valid behaviour that we've lived with to date.

@wing328 wing328 merged commit c0a6eaa into OpenAPITools:master Sep 20, 2018
@wing328 wing328 added this to the 3.3.0 milestone Sep 20, 2018
@jmini
Copy link
Member

jmini commented Sep 21, 2018

a5b6486 (removing trailing slash from contextPath) impacts the @ApplicationPath value in some JAXRS example.

See #850 (comment) for reference.

I think the samples should be regenerated (and the java files containing the change need to be deleted before generation)

jaumard pushed a commit to jaumard/openapi-generator that referenced this pull request Sep 21, 2018
* Trim trailing '/' from basePathWithoutHost

Given that the normal use is {{{basePathWithoutHost}}}{{{path}}}.

* Trim trailing '/' from contextPath
@bjgill bjgill deleted the trim-basePathWithoutHost branch September 24, 2018 11:05
@wing328
Copy link
Member

wing328 commented Oct 2, 2018

@bjgill thanks again for the PR, which is included in the v3.3.0 minor release: https://twitter.com/oas_generator/status/1046941449609068544

A-Joshi pushed a commit to ihsmarkitoss/openapi-generator that referenced this pull request Feb 27, 2019
* Trim trailing '/' from basePathWithoutHost

Given that the normal use is {{{basePathWithoutHost}}}{{{path}}}.

* Trim trailing '/' from contextPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants