Skip to content

Commit 2aa8a6d

Browse files
authored
[core] Log some things in model/url utils once (#5233)
1 parent 4602596 commit 2aa8a6d

2 files changed

Lines changed: 17 additions & 15 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.util.Map.Entry;
4040
import java.util.stream.Collectors;
4141

42+
import static org.openapitools.codegen.utils.OnceLogger.once;
43+
4244
public class ModelUtils {
4345
private static final Logger LOGGER = LoggerFactory.getLogger(ModelUtils.class);
4446

@@ -248,7 +250,7 @@ private static void visitParameters(OpenAPI openAPI, List<Parameter> parameters,
248250
}
249251
visitContent(openAPI, parameter.getContent(), visitor, visitedSchemas);
250252
} else {
251-
LOGGER.warn("Unreferenced parameter found.");
253+
once(LOGGER).warn("Unreferenced parameter(s) found.");
252254
}
253255
}
254256
}
@@ -329,7 +331,7 @@ public static String getSimpleRef(String ref) {
329331
} else if (ref.startsWith("#/definitions/")) {
330332
ref = ref.substring(ref.lastIndexOf("/") + 1);
331333
} else {
332-
LOGGER.warn("Failed to get the schema name: {}", ref);
334+
once(LOGGER).warn("Failed to get the schema name: {}", ref);
333335
//throw new RuntimeException("Failed to get the schema: " + ref);
334336
return null;
335337

@@ -561,7 +563,8 @@ public static boolean isEmailSchema(Schema schema) {
561563
*/
562564
public static boolean isModel(Schema schema) {
563565
if (schema == null) {
564-
LOGGER.error("Schema cannot be null in isModel check");
566+
// TODO: Is this message necessary? A null schema is not a model, so the result is correct.
567+
once(LOGGER).error("Schema cannot be null in isModel check");
565568
return false;
566569
}
567570

@@ -571,11 +574,7 @@ public static boolean isModel(Schema schema) {
571574
}
572575

573576
// composed schema is a model
574-
if (schema instanceof ComposedSchema) {
575-
return true;
576-
}
577-
578-
return false;
577+
return schema instanceof ComposedSchema;
579578
}
580579

581580
/**
@@ -586,7 +585,8 @@ public static boolean isModel(Schema schema) {
586585
*/
587586
public static boolean isFreeFormObject(Schema schema) {
588587
if (schema == null) {
589-
LOGGER.error("Schema cannot be null in isFreeFormObject check");
588+
// TODO: Is this message necessary? A null schema is not a free-form object, so the result is correct.
589+
once(LOGGER).error("Schema cannot be null in isFreeFormObject check");
590590
return false;
591591
}
592592

@@ -841,7 +841,7 @@ public static Schema unaliasSchema(OpenAPI openAPI, Schema schema) {
841841
if (schema != null && StringUtils.isNotEmpty(schema.get$ref())) {
842842
Schema ref = allSchemas.get(ModelUtils.getSimpleRef(schema.get$ref()));
843843
if (ref == null) {
844-
LOGGER.warn("{} is not defined", schema.get$ref());
844+
once(LOGGER).warn("{} is not defined", schema.get$ref());
845845
return schema;
846846
} else if (ref.getEnum() != null && !ref.getEnum().isEmpty()) {
847847
// top-level enum class

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/URLPathUtils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.util.regex.Matcher;
3434
import java.util.regex.Pattern;
3535

36+
import static org.openapitools.codegen.utils.OnceLogger.once;
37+
3638
public class URLPathUtils {
3739

3840
private static final Logger LOGGER = LoggerFactory.getLogger(URLPathUtils.class);
@@ -43,7 +45,7 @@ public class URLPathUtils {
4345
public static URL getServerURL(OpenAPI openAPI, Map<String, String> userDefinedVariables) {
4446
final List<Server> servers = openAPI.getServers();
4547
if (servers == null || servers.isEmpty()) {
46-
LOGGER.warn("Server information seems not defined in the spec. Default to {}.", LOCAL_HOST);
48+
once(LOGGER).warn("Server information seems not defined in the spec. Default to {}.", LOCAL_HOST);
4749
return getDefaultUrl();
4850
}
4951
// TODO need a way to obtain all server URLs
@@ -66,7 +68,7 @@ public static URL getServerURL(final Server server, final Map<String, String> us
6668
try {
6769
return new URL(url);
6870
} catch (MalformedURLException e) {
69-
LOGGER.warn("Not valid URL: {}. Default to {}.", server.getUrl(), LOCAL_HOST);
71+
once(LOGGER).warn("Not valid URL: {}. Default to {}.", server.getUrl(), LOCAL_HOST);
7072
}
7173
}
7274
return getDefaultUrl();
@@ -205,10 +207,10 @@ private static String sanitizeUrl(String url) {
205207
if (url != null) {
206208
if (url.startsWith("//")) {
207209
url = "http:" + url;
208-
LOGGER.warn("'scheme' not defined in the spec (2.0). Default to [http] for server URL [{}]", url);
210+
once(LOGGER).warn("'scheme' not defined in the spec (2.0). Default to [http] for server URL [{}]", url);
209211
} else if (url.startsWith("/")) {
210212
url = LOCAL_HOST + url;
211-
LOGGER.warn("'host' (OAS 2.0) or 'servers' (OAS 3.0) not defined in the spec. Default to [{}] for server URL [{}]", LOCAL_HOST, url);
213+
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);
212214
} else if (!url.matches("[a-zA-Z][0-9a-zA-Z.+\\-]+://.+")) {
213215
// Add http scheme for urls without a scheme.
214216
// 2.0 spec is restricted to the following schemes: "http", "https", "ws", "wss"
@@ -217,7 +219,7 @@ private static String sanitizeUrl(String url) {
217219
// can have alpha-numeric characters and [.+-]. Examples are here:
218220
// https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
219221
url = "http://" + url;
220-
LOGGER.warn("'scheme' not defined in the spec (2.0). Default to [http] for server URL [{}]", url);
222+
once(LOGGER).warn("'scheme' not defined in the spec (2.0). Default to [http] for server URL [{}]", url);
221223
}
222224
}
223225
return url;

0 commit comments

Comments
 (0)