Skip to content

swagger-core treats relative refs without leading "." as internal refs #2138

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

Open
mkistler opened this issue Mar 9, 2017 · 4 comments
Open
Assignees

Comments

@mkistler
Copy link

mkistler commented Mar 9, 2017

If the value of a $ref is a relative reference but does not start with ".", the swagger core (specifically GenericRef) treats it as an internal ref. E.g.

"$ref" : "./path/to/model.json#/thing"

is treated as a relative URI but

"$ref": "path/to/model.json#/thing"

is treated as internal.

I did research the syntax of $ref -- starting from the Swagger 2.0 spec, then to the JSON Schema spec, ending in IETF RFC 3986 Uniform Resource Identifier (URI): Generic Syntax -- and as best I can tell, a relative reference does not need to start with "." to be considered a relative reference.

@mkistler
Copy link
Author

mkistler commented Mar 9, 2017

Just found #1977 which is more or less a dup of this.

Unfortunately, the fix (in swagger-api/swagger-parser#307) appears to only work for properties, and not paths, parameters, definitions, or responses.

@fehguy
Copy link
Contributor

fehguy commented Mar 9, 2017

I'm not sure why this would be in this project. Are you parsing a specification that already exists? If so, please open this on the parser. Swagger core won't generate any $ref values like what you're describing.

@mkistler
Copy link
Author

mkistler commented Mar 9, 2017

I wrote the bug against this project because It is theGenericRef class in this project that is broken, and this can be seen by adding tests for the cases I described to the GenericRefsTests. E.g.,

    @Test(description = "it should correctly identify relative refs")
    public void identifyRelativeRefs() {
        assertRefFormat(new GenericRef(RefType.DEFINITION, "./path/to/model.json"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.DEFINITION, "./path/to/model.json#/thing"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.DEFINITION, "../path/to/model.json"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.DEFINITION, "../path/to/model.json#/thing"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.DEFINITION, "path/to/model.json"), RefFormat.RELATIVE);   // fails
        assertRefFormat(new GenericRef(RefType.DEFINITION, "path/to/model.json#/thing"), RefFormat.RELATIVE); // fails
        assertRefFormat(new GenericRef(RefType.PARAMETER, "./path/to/model.json"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.PARAMETER, "./path/to/model.json#/thing"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.PARAMETER, "../path/to/model.json"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.PARAMETER, "../path/to/model.json#/thing"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.PARAMETER, "path/to/model.json"), RefFormat.RELATIVE);  // fails
        assertRefFormat(new GenericRef(RefType.PARAMETER, "path/to/model.json#/thing"), RefFormat.RELATIVE);  // fails
        assertRefFormat(new GenericRef(RefType.RESPONSE, "./path/to/model.json"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.RESPONSE, "./path/to/model.json#/thing"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.RESPONSE, "../path/to/model.json"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.RESPONSE, "../path/to/model.json#/thing"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.RESPONSE, "path/to/model.json"), RefFormat.RELATIVE);  // fails
        assertRefFormat(new GenericRef(RefType.RESPONSE, "path/to/model.json#/thing"), RefFormat.RELATIVE);  // fails
        assertRefFormat(new GenericRef(RefType.PATH, "./path/to/model.json"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.PATH, "./path/to/model.json#/thing"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.PATH, "../path/to/model.json"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.PATH, "../path/to/model.json#/thing"), RefFormat.RELATIVE);
        assertRefFormat(new GenericRef(RefType.PATH, "path/to/model.json"), RefFormat.RELATIVE);  // fails
        assertRefFormat(new GenericRef(RefType.PATH, "path/to/model.json#/thing"), RefFormat.RELATIVE);  // fails
    }

And, yes, I am parsing an existing specification. So fixing this in the parser as was done for #1977 is sufficient for my purposes ... as long as it is implemented for all the element types.

@marcelstoer
Copy link

I'm not sure why this would be in this project [...] please open this on the parser.

It actually started over in the parser project. For affected users it doesn't matter much where it's fixed - as long as it gets fixed at all.

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

4 participants