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

How to add/remove extensions #678

Open
danielSenpaiDev opened this issue Sep 4, 2023 · 4 comments
Open

How to add/remove extensions #678

danielSenpaiDev opened this issue Sep 4, 2023 · 4 comments
Assignees

Comments

@danielSenpaiDev
Copy link

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.

@EricWittmann
Copy link
Member

EricWittmann commented Sep 11, 2023

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!

@EricWittmann
Copy link
Member

Note: there are two improvements that need to be made that are on my list:

  1. The code generator we're using for version 2.0 doesn't correctly push common interfaces (like Extensible) up the data model hierarchy the way it should. That is why you cannot just do this:

document.getInfo().getExtensions()

  1. The extensions are all JsonNode when using the library in Java. It would be nice to have variants of addExtension for common types (or at least String) to make things easier.

To address (2) I'll probably add a String variant of addExtension in the Extensible interface, which currently looks like this:

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 JsonUtil to make creating JsonNode objects easier.

If you're using the library in typescript, you can just use normal types and the current variants are fine.

@EricWittmann EricWittmann self-assigned this Sep 11, 2023
@danielSenpaiDev
Copy link
Author

danielSenpaiDev commented Sep 11, 2023

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!

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();
jnode = mapper.readTree(json);
((ObjectNode)jnode.get("info")).put("x-extension-example", "hello");
json = mapper.writeValueAsString(jnode);

json node now have the field x-extension-example.

@EricWittmann
Copy link
Member

Can you explain a little bit more about your use case? I'd be interested in what you're actually trying to accomplish.

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

2 participants