From 2aa8a6d033699121d2692a3b03993748b3eeb3ed Mon Sep 17 00:00:00 2001 From: Jim Schubert Date: Sun, 9 Feb 2020 00:19:25 -0500 Subject: [PATCH] [core] Log some things in model/url utils once (#5233) --- .../codegen/utils/ModelUtils.java | 20 +++++++++---------- .../codegen/utils/URLPathUtils.java | 12 ++++++----- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 2915e039f1dc..feda5fbe861d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -39,6 +39,8 @@ import java.util.Map.Entry; import java.util.stream.Collectors; +import static org.openapitools.codegen.utils.OnceLogger.once; + public class ModelUtils { private static final Logger LOGGER = LoggerFactory.getLogger(ModelUtils.class); @@ -248,7 +250,7 @@ private static void visitParameters(OpenAPI openAPI, List parameters, } visitContent(openAPI, parameter.getContent(), visitor, visitedSchemas); } else { - LOGGER.warn("Unreferenced parameter found."); + once(LOGGER).warn("Unreferenced parameter(s) found."); } } } @@ -329,7 +331,7 @@ public static String getSimpleRef(String ref) { } else if (ref.startsWith("#/definitions/")) { ref = ref.substring(ref.lastIndexOf("/") + 1); } else { - LOGGER.warn("Failed to get the schema name: {}", ref); + once(LOGGER).warn("Failed to get the schema name: {}", ref); //throw new RuntimeException("Failed to get the schema: " + ref); return null; @@ -561,7 +563,8 @@ public static boolean isEmailSchema(Schema schema) { */ public static boolean isModel(Schema schema) { if (schema == null) { - LOGGER.error("Schema cannot be null in isModel check"); + // TODO: Is this message necessary? A null schema is not a model, so the result is correct. + once(LOGGER).error("Schema cannot be null in isModel check"); return false; } @@ -571,11 +574,7 @@ public static boolean isModel(Schema schema) { } // composed schema is a model - if (schema instanceof ComposedSchema) { - return true; - } - - return false; + return schema instanceof ComposedSchema; } /** @@ -586,7 +585,8 @@ public static boolean isModel(Schema schema) { */ public static boolean isFreeFormObject(Schema schema) { if (schema == null) { - LOGGER.error("Schema cannot be null in isFreeFormObject check"); + // TODO: Is this message necessary? A null schema is not a free-form object, so the result is correct. + once(LOGGER).error("Schema cannot be null in isFreeFormObject check"); return false; } @@ -841,7 +841,7 @@ public static Schema unaliasSchema(OpenAPI openAPI, Schema schema) { if (schema != null && StringUtils.isNotEmpty(schema.get$ref())) { Schema ref = allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref())); if (ref == null) { - LOGGER.warn("{} is not defined", schema.get$ref()); + once(LOGGER).warn("{} is not defined", schema.get$ref()); return schema; } else if (ref.getEnum() != null && !ref.getEnum().isEmpty()) { // top-level enum class diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java index e1d3611fedf2..d2babe91a0c8 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java @@ -33,6 +33,8 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static org.openapitools.codegen.utils.OnceLogger.once; + public class URLPathUtils { private static final Logger LOGGER = LoggerFactory.getLogger(URLPathUtils.class); @@ -43,7 +45,7 @@ public class URLPathUtils { public static URL getServerURL(OpenAPI openAPI, Map userDefinedVariables) { final List servers = openAPI.getServers(); if (servers == null || servers.isEmpty()) { - LOGGER.warn("Server information seems not defined in the spec. Default to {}.", LOCAL_HOST); + once(LOGGER).warn("Server information seems not defined in the spec. Default to {}.", LOCAL_HOST); return getDefaultUrl(); } // TODO need a way to obtain all server URLs @@ -66,7 +68,7 @@ public static URL getServerURL(final Server server, final Map us try { return new URL(url); } catch (MalformedURLException e) { - LOGGER.warn("Not valid URL: {}. Default to {}.", server.getUrl(), LOCAL_HOST); + once(LOGGER).warn("Not valid URL: {}. Default to {}.", server.getUrl(), LOCAL_HOST); } } return getDefaultUrl(); @@ -205,10 +207,10 @@ private static String sanitizeUrl(String url) { if (url != null) { if (url.startsWith("//")) { url = "http:" + url; - LOGGER.warn("'scheme' not defined in the spec (2.0). Default to [http] for server URL [{}]", url); + once(LOGGER).warn("'scheme' not defined in the spec (2.0). Default to [http] for server URL [{}]", url); } else if (url.startsWith("/")) { url = LOCAL_HOST + url; - LOGGER.warn("'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [{}] for server URL [{}]", LOCAL_HOST, url); + once(LOGGER).warn("'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [{}] for server URL [{}]", LOCAL_HOST, url); } else if (!url.matches("[a-zA-Z][0-9a-zA-Z.+\\-]+://.+")) { // Add http scheme for urls without a scheme. // 2.0 spec is restricted to the following schemes: "http", "https", "ws", "wss" @@ -217,7 +219,7 @@ private static String sanitizeUrl(String url) { // can have alpha-numeric characters and [.+-]. Examples are here: // https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml url = "http://" + url; - LOGGER.warn("'scheme' not defined in the spec (2.0). Default to [http] for server URL [{}]", url); + once(LOGGER).warn("'scheme' not defined in the spec (2.0). Default to [http] for server URL [{}]", url); } } return url;