public OpenApiNormalizer(Options options) { this.options = options; debug("Normalizer Options", options); }
/** * Output base-level debug output, enabled whenever at least one named option is * enabled */ public static void debug(Object... args) { if (activeOptions.contains(Option.DEFAULT) || debugAll) { doDebug("*", args); } }
private static void doDebug(String optName, Object... args) { System.err.printf("NORMALIZER [%s] ==============================================================\n", optName); for (Object arg : args) { print(arg); } }
private static void print(Object obj) { if (obj == null) { System.err.println("null"); } else if (obj instanceof JsonNode) { printYaml(obj); } else if (obj instanceof Swagger) { printYaml(obj); } else if (obj instanceof OpenAPI) { printYaml(obj); } else if (obj instanceof Throwable) { printThrowable((Throwable) obj); } else if (obj.getClass().isArray()) { List<String> elts = Lists.newArrayList(); for (Object elt : (Object[]) obj) { elts.add(elt.toString()); } System.err.println("[" + StringUtils.join(elts, ", \n") + "]"); } else { System.err.println(obj.toString()); } }
private static void fixSimpleRef(JsonNode node, String container) { String before = node.path("$ref").asText(); if (isSimpleRef(node)) { String after = String.format("#/%s/%s", container, before); debug(SIMPLE_REFS, String.format("Simple Ref: %s => %s", before, after)); ((ObjectNode) node).put("$ref", after); } else if (isFauxSimpleRef(node)) { // we fix up most ref strings that Swagger20Parser would incorrectly treat as // simple refs, by prefixing them // with "./". This matters only for unresolvable references, since other // references will be replaced as a // side-effect of resolution. String after = "./" + before; debug(SIMPLE_REFS, String.format("Faux Simple Ref: %s => %s", before, after)); ((ObjectNode) node).put("$ref", after); } }
/** * Output tagged to a named option */ public static void debug(Option option, Object... args) { if (activeOptions.contains(option) || debugAll) { doDebug(option.name(), args); } }
public JsonNode normalizeToJson(URL resolutionBase) throws IOException, GenerationException { long start = System.nanoTime(); try { resolutionBase = checkArgs(resolutionBase); debug("Normalizer Start", resolutionBase, Thread.currentThread().getStackTrace(), spec != null ? spec : tree); JsonNode normalized = normalizeToJsonInternal(resolutionBase); if (!options.isDeferExtensionDataRemoval()) { OpenApiMarkers.removeMarkers(normalized, options); } debug(FINAL_SPEC, "Normalized model spec", normalized); return normalized; } catch (IOException | GenerationException e) { debug(e); throw e; } finally { long elapsed = (System.nanoTime() - start) / (1000 * 1000); // milliseconds debug(String.format("Normalizer End [%d.%03dsec]", elapsed / 1000, (elapsed % 1000))); } }
public OpenApi<?> normalizeToKaizen(URL resolutionBase) throws IOException, GenerationException { String baseUrlString = new Reference(resolutionBase, options.getModelVersion()).getCanonicalFileRefString(); options.resolveScope(baseUrlString); debug("Normalizer Start", resolutionBase, Thread.currentThread().getStackTrace(), spec != null ? spec : tree); long start = System.nanoTime(); tree = normalizeToJsonInternal(resolutionBase); try { debug(NormalizerDebug.Option.PRE_PARSE_SPEC, tree); String spec = mapper.writeValueAsString(tree); return new OpenApiParser().parse(spec, resolutionBase, true); } catch (Exception e) { debug(e); throw e; } finally { long elapsed = (System.nanoTime() - start) / (1000 * 1000); // milliseconds debug(String.format("Normalizer End [%d.%03dsec]", elapsed / 1000, (elapsed % 1000))); } }
public OpenAPI normalizeToOpenAPI(URL resolutionBase) throws IOException, GenerationException { String baseUrlString = new Reference(resolutionBase, options.getModelVersion()).getCanonicalFileRefString(); options.resolveScope(baseUrlString); debug("Normalizer Start", resolutionBase, Thread.currentThread().getStackTrace(), spec != null ? spec : tree); long start = System.nanoTime(); tree = normalizeToJsonInternal(resolutionBase); debug(NormalizerDebug.Option.PRE_PARSE_SPEC, tree); OpenAPI model = result.getOpenAPI(); if (model != null) { debug(FINAL_SPEC, "Normalized OpenAPIv2 Spec", model); return model; } else { debug(e); throw e; } finally { long elapsed = (System.nanoTime() - start) / (1000 * 1000); // milliseconds debug(String.format("Normalizer End [%d.%03dsec]", elapsed / 1000, (elapsed % 1000)));
public Swagger normalizeToSwagger(URL resolutionBase) throws IOException, GenerationException { String baseUrlString = new Reference(resolutionBase, SWAGGER_MODEL_VERSION).getCanonicalFileRefString(); options.resolveScope(baseUrlString); debug("Normalizer Start", resolutionBase, Thread.currentThread().getStackTrace(), spec != null ? spec : tree); long start = System.nanoTime(); tree = normalizeToJsonInternal(resolutionBase); debug(NormalizerDebug.Option.PRE_PARSE_SPEC, tree); debug(NormalizerDebug.Option.POST_PARSE_SPEC, tree); debug(FINAL_SPEC, "Normalized Swagger Spec", model); return model; } catch (Exception e) { debug(e); throw e; } finally { long elapsed = (System.nanoTime() - start) / (1000 * 1000); // milliseconds debug(String.format("Normalizer End [%d.%03dsec]", elapsed / 1000, (elapsed % 1000)));