-
Notifications
You must be signed in to change notification settings - Fork 38
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
How to add/remove extensions #678
Comments
Extensions are still supported in version 2, but they are a little harder to use, I will admit. I should probably add some convenience methods to make it easier, but here's an example of what you can do in v2: public class LibraryTest {
private static final String OPENAPI = "{\n"
+ " \"openapi\": \"3.0.2\",\n"
+ " \"info\": {\n"
+ " \"title\": \"Info With Extensions\",\n"
+ " \"version\": \"1.0.0\",\n"
+ " \"description\": \"\",\n"
+ " \"x-extension-example\": \"hello world\"\n"
+ " }\n"
+ "}";
@Test
public void testInfoWithExtensions() {
OpenApi30Document document = (OpenApi30Document) Library.readDocumentFromJSONString(OPENAPI);
Assert.assertEquals("3.0.2", document.getOpenapi());
OpenApi30Info info = (OpenApi30Info) document.getInfo();
Map<String, JsonNode> infoExtensions = info.getExtensions();
Assert.assertNotNull(infoExtensions);
Assert.assertFalse(infoExtensions.isEmpty());
Assert.assertEquals("hello world", info.getExtensions().get("x-extension-example").asText());
// Now add another extension
JsonNode extensionValue = JsonNodeFactory.instance.textNode("nice to see you!");
info.addExtension("x-extension-example-2", extensionValue);
Assert.assertEquals("nice to see you!", info.getExtensions().get("x-extension-example-2").asText());
}
} Let me know if that helps! |
Note: there are two improvements that need to be made that are on my list:
To address (2) I'll probably add a public interface Extensible {
public Map<String, JsonNode> getExtensions();
public void addExtension(String name, JsonNode value);
public void clearExtensions();
public void removeExtension(String name);
} I also might add some new methods to If you're using the library in typescript, you can just use normal types and the current variants are fine. |
Yes! in the meantime I'm adding nodes to the ObjectNode node json with put() method, given JsonNode is immutable. But basically it's the same. Something like this: ObjectMapper mapper = new ObjectMapper(); json node now have the field x-extension-example. |
Can you explain a little bit more about your use case? I'd be interested in what you're actually trying to accomplish. |
Hi.
I have some trouble trying to manipulate the OpenAPI spec as my convenience.
With data models 1.1.27 I could use it like this:
document.info.removeExtension("x-custom-field");
document.info.addExtraProperty("x-custom-field", LocalDateTime.now().toString());
and it worked. I add/remove extensions because I need to.
But now in 2.0.4 this is impossible, I tried
document.getInfo() but removeExtension doesn't exist anymore. Neither, there is no getExtensions() method. Add and remove extra properties is not related with extensions anymore and there is no chance to work with them.
Is there a way to work with extensions with Data Models 2.0.4?
Kind regards.
The text was updated successfully, but these errors were encountered: