KaiZen-OpenApi-Parser icon indicating copy to clipboard operation
KaiZen-OpenApi-Parser copied to clipboard

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

Open lekkalapudi opened this issue 6 years ago • 2 comments

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

lekkalapudi avatar Oct 31 '18 23:10 lekkalapudi

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);
        }
    }
}

chris-brace avatar Apr 12 '19 16:04 chris-brace

Nice assist, @chris-brace . Thanks!

tedepstein avatar Apr 12 '19 16:04 tedepstein