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

Generate default documentation for registered routes #49

Open
tipsy opened this issue Oct 8, 2022 · 1 comment
Open

Generate default documentation for registered routes #49

tipsy opened this issue Oct 8, 2022 · 1 comment
Labels
enhancement New feature or request scope: runtime

Comments

@tipsy
Copy link
Member

tipsy commented Oct 8, 2022

The old plugin had support for default responses:

    OpenApiOptions().apply {
        defaultDocumentation { doc ->
            doc.json("500", ErrorResponse::class.java)
            doc.json("503", ErrorResponse::class.java)
        }
    }

These are responses which could occur on any endpoint.

@dzikoysk dzikoysk added the enhancement New feature or request label Oct 8, 2022
@dzikoysk dzikoysk changed the title Default responses ? Generate default documentation for registered routes Oct 18, 2022
@dzikoysk dzikoysk moved this to 📋 Backlog in Javalin OpenApi Nov 21, 2022
@richardstephens
Copy link

richardstephens commented Dec 4, 2022

I got this working in a rather hack-ish way by supplying a custom document processer.

        options.setDocumentProcessor(
                (ObjectNode document) -> {
                    document.get("paths")
                            .forEach(
                                    pathNode ->
                                            pathNode.forEach(
                                                    methodNode ->
                                                            mutateResponseNode(
                                                                    methodNode.get("responses"))));
                    return document.toPrettyString();
                });
    private static ObjectNode childNode(String key, ObjectNode child) {
        ObjectNode object = objectMapper.createObjectNode();
        object.set(key, child);
        return object;
    }

    private static void mutateResponseNode(JsonNode node) {
        ObjectNode mutableNode = (ObjectNode) node;
        ObjectNode schemaNode = objectMapper.createObjectNode();
        schemaNode.put("$ref", "#/components/schemas/ErrorResponse");
        ObjectNode error500 = childNode(
                        "content", childNode("application/json", childNode("schema", schemaNode)));
        error500.put("description", "Server error");
        ObjectNode error503 = childNode(
                        "content", childNode("application/json", childNode("schema", schemaNode)));
        error503.put("description", "Service unavailable");

        mutableNode.putIfAbsent("500", error500);
        mutableNode.putIfAbsent("503", error503);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request scope: runtime
Projects
Status: 📋 Backlog
Development

No branches or pull requests

3 participants