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

<newbie question>can i use this library to compose a full fledged swagger document from separate path snippets and schema snippets? #229

Open
lekkalapudi opened this issue Oct 31, 2018 · 2 comments

Comments

@lekkalapudi
Copy link

can i use this library to compose a full fledged swagger document from separate path snippets and schema snippets?

@chris-brace
Copy link

chris-brace commented Apr 12, 2019

basically, yes, you can. look up refs and then do something like this

package oas3;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.reprezen.jsonoverlay.Overlay;
import com.reprezen.jsonoverlay.SerializationOptions;
import com.reprezen.kaizen.oasparser.OpenApi3Parser;
import com.reprezen.kaizen.oasparser.model3.OpenApi3;
import com.reprezen.kaizen.oasparser.val.ValidationResults;

import java.io.FileWriter;
import java.net.URL;

public class Main
{
    public static void main(String[] args) throws Exception
    {
        if (args.length != 1)
        {
            throw new IllegalArgumentException("Must have 1 command line param of target location.");
        }

        final String outFile = args[0];

        if (outFile == null || outFile.isBlank())
        {
            throw new IllegalArgumentException("Initial argument must be a path.");
        }

        final URL uri = Main.class.getClassLoader().getResource("api.yaml");
        final OpenApi3 model = new OpenApi3Parser().parse(uri);

        if (!model.isValid())
        {
            throw new Exception("Invalid model.");
        }

        final JsonNode serial = Overlay.of(model).toJson(SerializationOptions.Option.FOLLOW_REFS);
        final String raw = new YAMLMapper().writerWithDefaultPrettyPrinter().writeValueAsString(serial);

        // If we get here, write the file.
        try (final FileWriter fw = new FileWriter(outFile, false))
        {
            fw.write(raw);
        }
    }
}

@tedepstein
Copy link
Contributor

Nice assist, @chris-brace . Thanks!

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

3 participants