diff --git a/src/main/java/gov/usgs/earthquake/nshmp/netcdf/swagger/UpdateSwagger.java b/src/main/java/gov/usgs/earthquake/nshmp/netcdf/swagger/UpdateSwagger.java new file mode 100644 index 0000000000000000000000000000000000000000..63cec2ffdbb38596e8adc678b46b5ef7b03926d9 --- /dev/null +++ b/src/main/java/gov/usgs/earthquake/nshmp/netcdf/swagger/UpdateSwagger.java @@ -0,0 +1,150 @@ +package gov.usgs.earthquake.nshmp.netcdf.swagger; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +import org.yaml.snakeyaml.DumperOptions; +import org.yaml.snakeyaml.Yaml; + +import com.google.common.io.Resources; + +import gov.usgs.earthquake.nshmp.netcdf.NshmGroup; + +import io.swagger.v3.oas.models.PathItem; + +/** + * Create Swagger files for each model and update context path. + */ +class UpdateSwagger { + + private static final URL SWAGGER_URL = Resources.getResource("swagger"); + private static final Path SWAGGER_DIR = Paths.get("swagger-files"); + private static final Path TARGET_FILE; + + static { + try { + var dir = Paths.get(SWAGGER_URL.toURI()); + TARGET_FILE = dir.resolve("netcdf-swagger.yml"); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } + + public static void main(String[] args) { + run(); + } + + private static void run() { + for (var nshmGroup : NshmGroup.values()) { + updateSwaggerFile(TARGET_FILE, nshmGroup.model()); + } + } + + private static void updateSwaggerFile(Path yamlPath, String contextPath) { + var options = new DumperOptions(); + options.setPrettyFlow(true); + options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); + + var yaml = new Yaml(options); + var out = SWAGGER_DIR.resolve("netcdf-swagger-" + contextPath + ".yml"); + + try { + var yamlFile = yaml.loadAs(new FileReader(yamlPath.toFile()), Swagger.class); + var paths = new io.swagger.v3.oas.models.Paths(); + + for (var entry : yamlFile.getPaths().entrySet()) { + paths.addPathItem("/" + contextPath + entry.getKey(), entry.getValue()); + } + + yamlFile.paths = paths; + yaml.dump(yamlFile, new FileWriter(out.toFile())); + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } catch (IOException e) { + throw new RuntimeException("Could not write to [" + out + "]"); + } + } + + public static class Swagger { + String openapi = "3.0.1"; + Object info; + Map<String, PathItem> paths; + Object externalDocs; + Object servers; + Object tags; + Object components; + Map<String, Object> extensions; + + public String getOpenapi() { + return openapi; + } + + public Object getInfo() { + return info; + } + + public Map<String, PathItem> getPaths() { + return paths; + } + + public Object getExternalDocs() { + return externalDocs; + } + + public Object getServers() { + return servers; + } + + public Object getTags() { + return tags; + } + + public Object getComponents() { + return components; + } + + public Map<String, Object> getExtension() { + return extensions; + } + + public void setOpenapi(String openapi) { + this.openapi = openapi; + } + + public void setInfo(Object info) { + this.info = info; + } + + public void setPaths(Map<String, PathItem> paths) { + this.paths = paths; + } + + public void setExternalDocs(Object externalDocs) { + this.externalDocs = externalDocs; + } + + public void setServers(Object servers) { + this.servers = servers; + } + + public void setTags(Object tags) { + this.tags = tags; + } + + public void setComponents(Object components) { + this.components = components; + } + + public void setExtension(Map<String, Object> extensions) { + this.extensions = extensions; + } + } + +}