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

Fixes additionalProperties for v2 (swagger) specs #1414

Closed

Conversation

spacether
Copy link
Contributor

@spacether spacether commented Aug 3, 2020

Fixes #1369
Brings additionalProperties values into compliance with the Openapi 2.0 specification
Fixes two CI tests which were failing. They were failing because their requests to get json files from a url were not mocked and the requested url resource was returning a http 302 status code without the spec content.

Here is what the code in the v1 branch has done before this PR:

  • additionalProperties = true -> null
  • additionalProperties = false -> null
  • additionalProperties = string schema
  • additionalProperties = refed schema
  • additionalProperties omitted -> null

If this fix is merged, the code will now do the following:

  • additionalProperties = true -> {} (any type)
  • additionalProperties = false -> null
  • additionalProperties = string schema
  • additionalProperties = refed schema
  • additionalProperties omitted -> {} (any type)

TODO, update to:

  • additionalProperties = true -> Boolean.TRUE
  • additionalProperties = false -> Boolean.TRUE
  • additionalProperties = string schema
  • additionalProperties = refed schema
  • additionalProperties omitted -> null
    This will bring our results in line with the v3 spec parsing.

Future Thoughts

In the future it would be helpful if parsing of v2 and v3 specs resulted in:

  • additionalProperties = true -> {} (any type)
  • additionalProperties = false -> null
  • additionalProperties = string schema
  • additionalProperties = refed schema
  • additionalProperties omitted -> {} (any type)
    Because that would comply with the Openapi v2 and v3 specifications. Before that though, we should aim for consistency of results when parsing v2 and v3 specs.

This fix is applied to the v1 branch, and should rev roll it to a new version, maybe 1.1.0 because this may be a breaking change for legacy usages of additionalProperties which did not handle true/false/and omitted correctly.
We can't rev roll to 2.X.X because that is used by the current master branch which handles v3 specs and uses this branch for v2 spec parsing before conversion to v3 specs.

@sebastien-rosset
@gracekarina
@hkosova

@spacether spacether changed the title Adds fix for additionalProperties Adds fix for additionalProperties for v2 (swagger) specs Aug 3, 2020
@spacether spacether changed the title Adds fix for additionalProperties for v2 (swagger) specs Fixes additionalProperties for v2 (swagger) specs Aug 3, 2020
@@ -819,6 +820,10 @@ public Model definition(ObjectNode node, String location, ParseResult result) {
JsonNode ap = node.get("additionalProperties");
if (ap != null && ap.getNodeType().equals(JsonNodeType.OBJECT)) {
impl.setAdditionalProperties(Json.mapper().convertValue(ap, Property.class));
} else if ("object".equalsIgnoreCase(type) && (ap == null || "true".equalsIgnoreCase((String) ap.asText()))) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we set additionalProperties true for composed schemas also?
What does the master branch of swagger parser do?

@spacether
Copy link
Contributor Author

spacether commented Aug 4, 2020

@gracekarina can you help debug these failures?
This other PR that I just opened with no functional changes on the v1 branch also has these same 2 test failures. This shows that these failures are unrelated to my change, right?

@spacether
Copy link
Contributor Author

@gracekarina can you help debug these failures?

Never mind; I was able to fix them on my own.
@gracekarina all tests pass. Can we get a review on this PR?

assertNotNull(property);
assertTrue(property instanceof RefProperty);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved + response mocked to get test to pass

Copy link
Contributor

Choose a reason for hiding this comment

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

I have a question why the need to move the test from one class to another?

Copy link
Contributor Author

@spacether spacether Aug 8, 2020

Choose a reason for hiding this comment

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

It was easier to do it this way. All of our classes that we need to attach the fake request already exist in the file that I moved it in to. If I kept them where they were then I would need to add new import and setup lines for mocking the request for just those two tests in two separate files. We already have that set up code in the file that I moved them into.

Also, because these two tests need network resources, these are NetworkReferenceTests


assertTrue(swagger.getDefinitions().get("Order") instanceof ModelImpl);
assertTrue(swagger.getDefinitions().get("Order").getProperties().size() == 6);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved + response mocked to get test to pass

assertNotNull(schema);
assertNotNull(schema.getAdditionalProperties());
assertEquals(schema.getAdditionalProperties().getType(), "ref");
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these are our additionalProperties tests

@spacether spacether marked this pull request as draft August 25, 2020 06:06
@spacether
Copy link
Contributor Author

Converting to draft while I am making these changes:
TODO, update to:

additionalProperties = true -> Boolean.TRUE
additionalProperties = false -> Boolean.TRUE
additionalProperties = string schema
additionalProperties = refed schema
additionalProperties omitted -> null
This will bring our results in line with the v3 spec parsing.

@spacether
Copy link
Contributor Author

Making the TODO changes was too arduous so I stopped working on this.

@spacether
Copy link
Contributor Author

Closing this because there doesn't appear to be interest from the swagger-parser team in merging it and feedback in #1369 is that the v2 openapi spec definition of additionalProperties is not consistent with that the openapi spec authors intended.

@spacether spacether closed this Jan 28, 2021
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

Successfully merging this pull request may close these issues.

2 participants