swagger-api / swagger-parser

Swagger Spec to Java POJOs
http://swagger.io
Apache License 2.0
778 stars 525 forks source link

swagger-parser-v3 failed on Windows for relative $ref #1511

Open cdestombes opened 3 years ago

cdestombes commented 3 years ago

On Windows for swagger-parver-v3 v2.0.24, we have StringIndexOutOfBoundsException for relative $ref like $ref: './path/file.yaml#/components/schemas/Type'

Parsing problem is on ExternalRefProcessor.processRefToExternalSchema at line 113 String parent = file.substring(0, file.lastIndexOf(File.separatorChar));

I don't think that OS specific separator need to be use here. In yaml, all paths are with '/' no ? I think only lastIndexOf '/' is needed : String parent = file.substring(0, file.lastIndexOf('/'));

But after this change, we have other problem on : schemaFullRef = Paths.get(parent, schemaFullRef).normalize().toString();

After normalise relative in file part "#/components/schemas/Type" is also convert to : path\file.yaml#\components\schemas\Type

I made some change for adjust that with :

if (schemaFullRef.contains("#/")) {
    String[] parts = schemaFullRef.split("#/");
    String schemaFullRefFilePart = parts[0];
    String schemaFullRefInternalRefPart = parts[1];
    schemaFullRef = Paths.get(parent, schemaFullRefFilePart).normalize().toString() + "#/" + schemaFullRefInternalRefPart;
} else {
    schemaFullRef = Paths.get(parent, schemaFullRef).normalize().toString();
}

This changes made build OK on Windows and Linux but I'm not sure to cover all case. Someone see a better approch for a PR ?

gracekarina commented 3 years ago

Hi @cdestombes please submit a PR, with the test case and proposed solution, so we can review.

Thanks!

wasanya commented 3 years ago

Example failed_on_windows_for_relative_ref.zip

cdestombes commented 3 years ago

Hi, sorry for long delay before reply. I was busy on other projects.

I juste create a PR https://github.com/swagger-api/swagger-parser/pull/1575 for build on Windows with changes proposed initialy. I wasn't able to fix all tests problems for build on Windows. Two tests still failed :

For both tests, the failed is on : assertTrue(definitions.containsKey("greeting-message"));

I don't understood why this key is expected. Should be came from filename ? I don't see any element "greeting-message" in yaml.