Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some sonar warnings #1645

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,6 @@ public void visit(TableCaptionNode node) {

@Override
public void visit(TableCellNode node) {
// String tag = inTableHeader ? "th" : "td";
List<TableColumnNode> columns = currentTableNode.getColumns();
TableColumnNode column = columns.get(Math.min(currentTableColumn, columns.size() - 1));

Expand Down Expand Up @@ -439,7 +438,6 @@ public void visit(TableColumnNode node) {
@Override
public void visit(TableHeaderNode node) {
inTableHeader = true;
// printIndentedTag(node, "thead");

visitChildren(node);

Expand Down Expand Up @@ -501,7 +499,6 @@ public void visit(TableRowNode node) {
printer.println();

visitChildren(node);
// printIndentedTag(node, "tr");

if (inTableHeader) {
printer.println();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,8 @@ public static PrimitiveType fromName(String name) {
return null;
}
PrimitiveType fromName = NAMES.get(name);
if (fromName == null) {
if (!customExcludedExternalClasses().contains(name)) {
fromName = EXTERNAL_CLASSES.get(name);
}
if (fromName == null && !customExcludedExternalClasses().contains(name)) {
fromName = EXTERNAL_CLASSES.get(name);
}
return fromName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ public String testFileFolder() {

@Override
public String apiTestFileFolder() {
return testFileFolder() + apiPackage().replaceAll("\\.", "/");
return testFileFolder() + apiPackage().replace(".", "/");
}

@Override
Expand Down Expand Up @@ -854,13 +854,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
// is only default "application/json" media type
if (op.consumes == null
|| op.consumes.isEmpty()
|| op.consumes.size() == 1 && "application/json".equals(op.consumes.get(0).get("mediaType"))) {
|| op.consumes.size() == 1 && CONTENT_TYPE_APPLICATION_JSON.equals(op.consumes.get(0).get("mediaType"))) {
op.vendorExtensions.put("onlyDefaultConsumeOrEmpty", true);
}
// is only default "application/json" media type
if (op.produces == null
|| op.produces.isEmpty()
|| op.produces.size() == 1 && "application/json".equals(op.produces.get(0).get("mediaType"))) {
|| op.produces.size() == 1 && CONTENT_TYPE_APPLICATION_JSON.equals(op.produces.get(0).get("mediaType"))) {
op.vendorExtensions.put("onlyDefaultProduceOrEmpty", true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ public void setApiPackage(String apiPackage) {
updateOption(CodegenConstants.API_PACKAGE, apiPackage);
}

@Override
public void setPackageName(String packageName) {
super.setPackageName(packageName);
updateOption(INVOKER_PACKAGE, packageName);
Expand Down Expand Up @@ -654,7 +655,7 @@ public String testFileFolder() {

@Override
public String apiTestFileFolder() {
return testFileFolder() + apiPackage().replaceAll("\\.", "/");
return testFileFolder() + apiPackage().replace(".", "/");
}

@Override
Expand Down Expand Up @@ -849,13 +850,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
// is only default "application/json" media type
if (op.consumes == null
|| op.consumes.isEmpty()
|| op.consumes.size() == 1 && "application/json".equals(op.consumes.get(0).get("mediaType"))) {
|| op.consumes.size() == 1 && CONTENT_TYPE_APPLICATION_JSON.equals(op.consumes.get(0).get("mediaType"))) {
op.vendorExtensions.put("onlyDefaultConsumeOrEmpty", true);
}
// is only default "application/json" media type
if (op.produces == null
|| op.produces.isEmpty()
|| op.produces.size() == 1 && "application/json".equals(op.produces.get(0).get("mediaType"))) {
|| op.produces.size() == 1 && CONTENT_TYPE_APPLICATION_JSON.equals(op.produces.get(0).get("mediaType"))) {
op.vendorExtensions.put("onlyDefaultProduceOrEmpty", true);
}

Expand Down Expand Up @@ -978,7 +979,7 @@ public String toModelName(final String name) {
return schemaMapping.get(name);
}

String modifiedName = name.replaceAll("\\.", "")
String modifiedName = name.replace(".", "")
.replace("-", "_")
// if it already escaped reserved word, need to remove quotes
.replace("`", "");
Expand Down Expand Up @@ -1683,7 +1684,7 @@ private String normalizeKotlinSpecificNames(final String name) {
}
String word = name;
for (Map.Entry<String, String> specialCharacters : specialCharReplacements.entrySet()) {
word = replaceSpecialCharacters(word, specialCharacters);
word = replaceSpecCharacters(word, specialCharacters);
}

// Fallback, replace unknowns with underscore.
Expand All @@ -1700,7 +1701,7 @@ private String normalizeKotlinSpecificNames(final String name) {
return word;
}

private String replaceSpecialCharacters(String word, Map.Entry<String, String> specialCharacters) {
private String replaceSpecCharacters(String word, Map.Entry<String, String> specialCharacters) {
String specialChar = specialCharacters.getKey();
String replacementChar = specialCharacters.getValue();
// Underscore is the only special character we'll allow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,8 +870,7 @@ private Schema modelFromProperty(OpenAPI openAPI, Schema object, String path) {
model.setExtensions(object.getExtensions());
model.setExclusiveMinimum(object.getExclusiveMinimum());
model.setExclusiveMaximum(object.getExclusiveMaximum());
// no need to set it again as it's set earlier
//model.setExample(object.getExample());
// no need to set example again as it's set earlier
model.setDeprecated(object.getDeprecated());

if (properties != null) {
Expand Down Expand Up @@ -977,22 +976,21 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
String propertyModelName = resolveModelName(property.getTitle(), path + "_" + key);
gatherInlineModels(property, propertyModelName);
propertyModelName = addSchemas(propertyModelName, property);
Schema schema = new Schema().$ref(propertyModelName);
schema.setRequired(property.getRequired());
var schema = new Schema<>()
.$ref(propertyModelName)
.required(property.getRequired());
propsToUpdate.put(key, schema);
}
} else {
LOGGER.debug("Schema not yet handled in model resolver: {}", property);
}
}
if (propsToUpdate.size() > 0) {
for (String key : propsToUpdate.keySet()) {
properties.put(key, propsToUpdate.get(key));
}
if (!propsToUpdate.isEmpty()) {
properties.putAll(propsToUpdate);
}
for (String key : modelsToAdd.keySet()) {
openAPI.getComponents().addSchemas(key, modelsToAdd.get(key));
this.addedModels.put(key, modelsToAdd.get(key));
for (var entry : modelsToAdd.entrySet()) {
openAPI.getComponents().addSchemas(entry.getKey(), entry.getValue());
this.addedModels.put(entry.getKey(), entry.getValue());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,30 @@ void clientOptsUnicity() {
codegen.cliOptions()
.stream()
.collect(groupingBy(CliOption::getOpt))
.forEach((k, v) -> assertEquals(v.size(), 1, k + " is described multiple times"));
.forEach((k, v) -> assertEquals(1, v.size(), k + " is described multiple times"));
}

@Test
void testInitialConfigValues() {
var codegen = new JavaMicronautClientCodegen();
codegen.processOpts();

OpenAPI openAPI = new OpenAPI();
var openAPI = new OpenAPI();
openAPI.addServersItem(new Server().url("https://one.com/v2"));
openAPI.setInfo(new Info());
codegen.preprocessOpenAPI(openAPI);

assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
assertFalse(codegen.isHideGenerationTimestamp());
assertEquals(codegen.modelPackage(), "org.openapitools.model");
assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "org.openapitools.model");
assertEquals(codegen.apiPackage(), "org.openapitools.api");
assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "org.openapitools.api");
assertEquals(codegen.getInvokerPackage(), "org.openapitools");
assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "org.openapitools");

assertEquals(Boolean.FALSE, codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP));
assertFalse(codegen.isHideGenerationTimestamp());
assertEquals("org.openapitools.model", codegen.modelPackage());
assertEquals("org.openapitools.model", codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE));
assertEquals("org.openapitools.api", codegen.apiPackage());
assertEquals("org.openapitools.api", codegen.additionalProperties().get(CodegenConstants.API_PACKAGE));
assertEquals("org.openapitools", codegen.getInvokerPackage());
assertEquals("org.openapitools", codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,28 @@ void clientOptsUnicity() {
codegen.cliOptions()
.stream()
.collect(groupingBy(CliOption::getOpt))
.forEach((k, v) -> assertEquals(v.size(), 1, k + " is described multiple times"));
.forEach((k, v) -> assertEquals(1, v.size(), k + " is described multiple times"));
}

@Test
void testInitialConfigValues() {
final var codegen = new JavaMicronautServerCodegen();
codegen.processOpts();

OpenAPI openAPI = new OpenAPI();
var openAPI = new OpenAPI();
openAPI.addServersItem(new Server().url("https://one.com/v2"));
openAPI.setInfo(new Info());
codegen.preprocessOpenAPI(openAPI);

assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
assertEquals(Boolean.FALSE, codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP));
assertFalse(codegen.isHideGenerationTimestamp());
assertEquals(codegen.modelPackage(), "org.openapitools.model");
assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "org.openapitools.model");
assertEquals(codegen.apiPackage(), "org.openapitools.api");
assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "org.openapitools.api");
assertEquals(codegen.additionalProperties().get(JavaMicronautServerCodegen.OPT_CONTROLLER_PACKAGE), "org.openapitools.controller");
assertEquals(codegen.getInvokerPackage(), "org.openapitools");
assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "org.openapitools");
assertEquals("org.openapitools.model", codegen.modelPackage());
assertEquals("org.openapitools.model", codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE));
assertEquals("org.openapitools.api", codegen.apiPackage());
assertEquals("org.openapitools.api", codegen.additionalProperties().get(CodegenConstants.API_PACKAGE));
assertEquals("org.openapitools.controller", codegen.additionalProperties().get(JavaMicronautServerCodegen.OPT_CONTROLLER_PACKAGE));
assertEquals("org.openapitools", codegen.getInvokerPackage());
assertEquals("org.openapitools", codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void clientOptsUnicity() {
codegen.cliOptions()
.stream()
.collect(groupingBy(CliOption::getOpt))
.forEach((k, v) -> assertEquals(v.size(), 1, k + " is described multiple times"));
.forEach((k, v) -> assertEquals(1, v.size(), k + " is described multiple times"));
}

@Test
Expand All @@ -35,14 +35,14 @@ void testInitialConfigValues() {
openAPI.setInfo(new Info());
codegen.preprocessOpenAPI(openAPI);

assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
assertEquals(Boolean.FALSE, codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP));
assertFalse(codegen.isHideGenerationTimestamp());
assertEquals(codegen.modelPackage(), "org.openapitools.model");
assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "org.openapitools.model");
assertEquals(codegen.apiPackage(), "org.openapitools.api");
assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "org.openapitools.api");
assertEquals(codegen.getPackageName(), "org.openapitools");
assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "org.openapitools");
assertEquals("org.openapitools.model", codegen.modelPackage());
assertEquals("org.openapitools.model", codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE));
assertEquals("org.openapitools.api", codegen.apiPackage());
assertEquals("org.openapitools.api", codegen.additionalProperties().get(CodegenConstants.API_PACKAGE));
assertEquals("org.openapitools", codegen.getPackageName());
assertEquals("org.openapitools", codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void clientOptsUnicity() {
codegen.cliOptions()
.stream()
.collect(groupingBy(CliOption::getOpt))
.forEach((k, v) -> assertEquals(v.size(), 1, k + " is described multiple times"));
.forEach((k, v) -> assertEquals(1, v.size(), k + " is described multiple times"));
}

@Test
Expand All @@ -36,15 +36,15 @@ void testInitialConfigValues() {
openAPI.setInfo(new Info());
codegen.preprocessOpenAPI(openAPI);

assertEquals(codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP), Boolean.FALSE);
assertEquals(Boolean.FALSE, codegen.additionalProperties().get(CodegenConstants.HIDE_GENERATION_TIMESTAMP));
assertFalse(codegen.isHideGenerationTimestamp());
assertEquals(codegen.modelPackage(), "org.openapitools.model");
assertEquals(codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE), "org.openapitools.model");
assertEquals(codegen.apiPackage(), "org.openapitools.api");
assertEquals(codegen.additionalProperties().get(CodegenConstants.API_PACKAGE), "org.openapitools.api");
assertEquals(codegen.additionalProperties().get(KotlinMicronautServerCodegen.OPT_CONTROLLER_PACKAGE), "org.openapitools.controller");
assertEquals(codegen.getPackageName(), "org.openapitools");
assertEquals(codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE), "org.openapitools");
assertEquals("org.openapitools.model", codegen.modelPackage());
assertEquals("org.openapitools.model", codegen.additionalProperties().get(CodegenConstants.MODEL_PACKAGE));
assertEquals("org.openapitools.api", codegen.apiPackage());
assertEquals("org.openapitools.api", codegen.additionalProperties().get(CodegenConstants.API_PACKAGE));
assertEquals("org.openapitools.controller", codegen.additionalProperties().get(JavaMicronautServerCodegen.OPT_CONTROLLER_PACKAGE));
assertEquals("org.openapitools", codegen.getPackageName());
assertEquals("org.openapitools", codegen.additionalProperties().get(CodegenConstants.INVOKER_PACKAGE));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import static io.micronaut.core.util.StringUtils.EMPTY_STRING;
import static io.micronaut.openapi.view.OpenApiViewConfig.replacePlaceHolder;
import static io.micronaut.openapi.visitor.StringUtil.DOT;
import static io.micronaut.openapi.visitor.StringUtil.SLASH;
Expand Down Expand Up @@ -226,7 +227,7 @@ private String toOauth2Options() {
if (StringUtils.hasText(properties)) {
return "ui.initOAuth({\n" + properties + "\n});";
} else {
return StringUtils.EMPTY_STRING;
return EMPTY_STRING;
}
}

Expand Down Expand Up @@ -266,28 +267,33 @@ public String render(String template, @Nullable VisitorContext context) {
String finalUrlPrefix = getFinalUrlPrefix(RendererType.SWAGGER_UI, context);

template = rapiPDFConfig.render(template, RendererType.SWAGGER_UI, context);
template = replacePlaceHolder(template, PREFIX_SWAGGER_UI + ".js.url.prefix", isDefaultJsUrl ? finalUrlPrefix : jsUrl, StringUtils.EMPTY_STRING);
template = replacePlaceHolder(template, PREFIX_SWAGGER_UI + ".attributes", toOptions(), StringUtils.EMPTY_STRING);
template = template.replace("{{" + PREFIX_SWAGGER_UI + ".theme}}", theme == null || Theme.CLASSIC == theme ? StringUtils.EMPTY_STRING :
"link(contextPath + \"" + (isDefaultThemeUrl ? finalUrlPrefix + theme.getCss() + ".css" : themeUrl) + "\", head, \"text/css\", \"stylesheet\")");
template = template.replace("{{" + PREFIX_SWAGGER_UI + DOT + OPTION_OAUTH2 + "}}", hasOauth2Option(options) ? toOauth2Options() : StringUtils.EMPTY_STRING);
template = template.replace("{{" + PREFIX_SWAGGER_UI + DOT + OPTION_PRIMARY_NAME + "}}", StringUtils.isNotEmpty(primaryName) ? getPrimaryName(context) : StringUtils.EMPTY_STRING);
template = replacePlaceHolder(template, PREFIX_SWAGGER_UI + ".js.url.prefix", isDefaultJsUrl ? finalUrlPrefix : jsUrl, EMPTY_STRING);
template = replacePlaceHolder(template, PREFIX_SWAGGER_UI + ".attributes", toOptions(), EMPTY_STRING);

if (theme != null && Theme.CLASSIC != theme) {
var themeCssLink = isDefaultThemeUrl ? finalUrlPrefix + theme.getCss() + ".css" : themeUrl;
template = template.replace("{{" + PREFIX_SWAGGER_UI + ".theme}}", "link(contextPath + \"" + themeCssLink + "\", head, \"text/css\", \"stylesheet\")");
} else {
template = template.replace("{{" + PREFIX_SWAGGER_UI + ".theme}}", EMPTY_STRING);
}
template = template.replace("{{" + PREFIX_SWAGGER_UI + DOT + OPTION_OAUTH2 + "}}", hasOauth2Option(options) ? toOauth2Options() : EMPTY_STRING);
template = template.replace("{{" + PREFIX_SWAGGER_UI + DOT + OPTION_PRIMARY_NAME + "}}", StringUtils.isNotEmpty(primaryName) ? getPrimaryName(context) : EMPTY_STRING);
template = template.replace("{{" + PREFIX_SWAGGER_UI + DOT + OPTION_URLS + "}}", getUrlStr(context));
return template;
}

@NonNull
private String getPrimaryName(VisitorContext context) {
if (StringUtils.isEmpty(primaryName)) {
return StringUtils.EMPTY_STRING;
return EMPTY_STRING;
}
return "\"urls.primaryName\":\"" + primaryName + "\",";
}

@NonNull
private String getUrlStr(VisitorContext context) {
if (CollectionUtils.isEmpty(urls) || (withUrls != null && !withUrls)) {
return StringUtils.EMPTY_STRING;
return EMPTY_STRING;
}

var isFirst = true;
Expand Down
Loading
Loading