swagger-api / swagger-api/swagger-parser
OpenApiDeserializer parses empty URL string as a validation error
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 867
- Forks
- 560
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 7
Description
Referencing to the issue #1619 , where empty strings are allowed, if we check the following code in version 2.0.31:
public Contact getContact(ObjectNode node, String location, ParseResult result) {
.....
value = getString("url", node, false, location, result);
if ((result.isAllowEmptyStrings() && value != null) || (!result.isAllowEmptyStrings() && !StringUtils.isBlank(value))) {
try {
new URL(value);
} catch (Exception e) {
result.warning(location, value);
}
contact.setUrl(value);
}
Info contact url:
https://github.com/swagger-api/swagger-parser/pull/1619/files#diff-6817a7685b85bf60e0859476689df9be7ef2433a4c3e5ffdaaf90ea562da03edR1003
This means that an empty url like the following will end in a validation error.
info:
contact:
url: ""
This is a breaking change since in previous versions if the contact url is empty, it does not add a warning. This is because the code
new URL("") will end in an exception because it is not a valid url.
All of this makes the need to disable the validation with skipValidateSpec, but it is not the best way I think, because I do not want to skip all the validations. I think that the best approach would be to recover the previous behaviour, which is that if the url is empty, not set the url, because it is a conflict to allow empty strings for urls in places where they are being validated by new URL("") call.
value = getString("url", node, false, location, result);
if (StringUtils.isNotBlank(value)) {
try {
new URL(value);
} catch (Exception e) {
result.warning(location, value);
}
contact.setUrl(value);
}
This would not set any url, it would be null.
I would be glad to contribute once determined the solution.
Thank you very much for your time :)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/OpenAPIDeserializer.java around the URL handling described near line 969, and compare the current behavior with the version 2.0.31 code linked in the issue. Reproduce parsing an OpenAPI document with an empty contact URL, then verify that empty URLs do not produce a validation warning and are left unset, including the related license URL behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, openapi
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 42/100