Skip to content

Commit

Permalink
[core] Log some things in model/url utils once (#5233)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimschubert committed Feb 9, 2020
1 parent 4602596 commit 2aa8a6d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
Expand Up @@ -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);

Expand Down Expand Up @@ -248,7 +250,7 @@ private static void visitParameters(OpenAPI openAPI, List<Parameter> parameters,
}
visitContent(openAPI, parameter.getContent(), visitor, visitedSchemas);
} else {
LOGGER.warn("Unreferenced parameter found.");
once(LOGGER).warn("Unreferenced parameter(s) found.");
}
}
}
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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);
Expand All @@ -43,7 +45,7 @@ public class URLPathUtils {
public static URL getServerURL(OpenAPI openAPI, Map<String, String> userDefinedVariables) {
final List<Server> 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
Expand All @@ -66,7 +68,7 @@ public static URL getServerURL(final Server server, final Map<String, String> 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();
Expand Down Expand Up @@ -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"
Expand All @@ -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;
Expand Down

0 comments on commit 2aa8a6d

Please sign in to comment.