diff --git a/openapi-adoc/src/main/java/io/micronaut/openapi/adoc/md/ToAsciiDocSerializer.java b/openapi-adoc/src/main/java/io/micronaut/openapi/adoc/md/ToAsciiDocSerializer.java index 11ebd96706..09d5e58d51 100644 --- a/openapi-adoc/src/main/java/io/micronaut/openapi/adoc/md/ToAsciiDocSerializer.java +++ b/openapi-adoc/src/main/java/io/micronaut/openapi/adoc/md/ToAsciiDocSerializer.java @@ -408,7 +408,6 @@ public void visit(TableCaptionNode node) { @Override public void visit(TableCellNode node) { -// String tag = inTableHeader ? "th" : "td"; List columns = currentTableNode.getColumns(); TableColumnNode column = columns.get(Math.min(currentTableColumn, columns.size() - 1)); @@ -439,7 +438,6 @@ public void visit(TableColumnNode node) { @Override public void visit(TableHeaderNode node) { inTableHeader = true; -// printIndentedTag(node, "thead"); visitChildren(node); @@ -501,7 +499,6 @@ public void visit(TableRowNode node) { printer.println(); visitChildren(node); -// printIndentedTag(node, "tr"); if (inTableHeader) { printer.println(); diff --git a/openapi-common/src/main/java/io/micronaut/openapi/swagger/core/util/PrimitiveType.java b/openapi-common/src/main/java/io/micronaut/openapi/swagger/core/util/PrimitiveType.java index 520da960cc..055f7329ea 100644 --- a/openapi-common/src/main/java/io/micronaut/openapi/swagger/core/util/PrimitiveType.java +++ b/openapi-common/src/main/java/io/micronaut/openapi/swagger/core/util/PrimitiveType.java @@ -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; } diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java index d41b962d60..7283427d4f 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/AbstractMicronautJavaCodegen.java @@ -606,7 +606,7 @@ public String testFileFolder() { @Override public String apiTestFileFolder() { - return testFileFolder() + apiPackage().replaceAll("\\.", "/"); + return testFileFolder() + apiPackage().replace(".", "/"); } @Override @@ -854,13 +854,13 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List specialCharacters : specialCharReplacements.entrySet()) { - word = replaceSpecialCharacters(word, specialCharacters); + word = replaceSpecCharacters(word, specialCharacters); } // Fallback, replace unknowns with underscore. @@ -1700,7 +1701,7 @@ private String normalizeKotlinSpecificNames(final String name) { return word; } - private String replaceSpecialCharacters(String word, Map.Entry specialCharacters) { + private String replaceSpecCharacters(String word, Map.Entry specialCharacters) { String specialChar = specialCharacters.getKey(); String replacementChar = specialCharacters.getValue(); // Underscore is the only special character we'll allow diff --git a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautInlineModelResolver.java b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautInlineModelResolver.java index 6cdcea1c48..6a7563cf85 100644 --- a/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautInlineModelResolver.java +++ b/openapi-generator/src/main/java/io/micronaut/openapi/generator/MicronautInlineModelResolver.java @@ -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) { @@ -977,22 +976,21 @@ private void flattenProperties(OpenAPI openAPI, Map 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()); } } diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java index 4585f6f610..70491f5e00 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautClientCodegenTest.java @@ -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 @@ -30,19 +30,22 @@ 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 diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautServerCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautServerCodegenTest.java index 9cb5e1af9a..636825c9e4 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautServerCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/JavaMicronautServerCodegenTest.java @@ -24,7 +24,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 @@ -32,20 +32,20 @@ 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 diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java index 35306e5a48..7cc8e2a011 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautClientCodegenTest.java @@ -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 @@ -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 diff --git a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java index aaad485a60..5784f9b75f 100644 --- a/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java +++ b/openapi-generator/src/test/java/io/micronaut/openapi/generator/KotlinMicronautServerCodegenTest.java @@ -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 @@ -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 diff --git a/openapi/src/main/java/io/micronaut/openapi/view/SwaggerUIConfig.java b/openapi/src/main/java/io/micronaut/openapi/view/SwaggerUIConfig.java index 5b133e3ac7..c5363dacc7 100644 --- a/openapi/src/main/java/io/micronaut/openapi/view/SwaggerUIConfig.java +++ b/openapi/src/main/java/io/micronaut/openapi/view/SwaggerUIConfig.java @@ -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; @@ -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; } } @@ -266,12 +267,17 @@ 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; } @@ -279,7 +285,7 @@ public String render(String template, @Nullable VisitorContext context) { @NonNull private String getPrimaryName(VisitorContext context) { if (StringUtils.isEmpty(primaryName)) { - return StringUtils.EMPTY_STRING; + return EMPTY_STRING; } return "\"urls.primaryName\":\"" + primaryName + "\","; } @@ -287,7 +293,7 @@ private String getPrimaryName(VisitorContext context) { @NonNull private String getUrlStr(VisitorContext context) { if (CollectionUtils.isEmpty(urls) || (withUrls != null && !withUrls)) { - return StringUtils.EMPTY_STRING; + return EMPTY_STRING; } var isFirst = true; diff --git a/openapi/src/main/java/io/micronaut/openapi/visitor/AbstractOpenApiEndpointVisitor.java b/openapi/src/main/java/io/micronaut/openapi/visitor/AbstractOpenApiEndpointVisitor.java index 653006dccc..496842bdc8 100644 --- a/openapi/src/main/java/io/micronaut/openapi/visitor/AbstractOpenApiEndpointVisitor.java +++ b/openapi/src/main/java/io/micronaut/openapi/visitor/AbstractOpenApiEndpointVisitor.java @@ -262,7 +262,7 @@ public void visitClass(ClassElement element, VisitorContext context) { private void processTags(ClassElement element, VisitorContext context) { classTags = readTags(element, context); - List userDefinedClassTags = classTags(element, context); + List userDefinedClassTags = getUserDefinedClassTags(element, context); if (CollectionUtils.isEmpty(classTags)) { classTags = userDefinedClassTags == null ? Collections.emptyList() : userDefinedClassTags; } else if (userDefinedClassTags != null) { @@ -318,7 +318,7 @@ private boolean containsTag(String name, List tags) { * * @return The class tags. */ - protected abstract List classTags(ClassElement element, VisitorContext context); + protected abstract List getUserDefinedClassTags(ClassElement element, VisitorContext context); /** * Returns true if the specified element should not be processed. diff --git a/openapi/src/main/java/io/micronaut/openapi/visitor/OpenApiControllerVisitor.java b/openapi/src/main/java/io/micronaut/openapi/visitor/OpenApiControllerVisitor.java index d16ce38b88..b27045aa1b 100644 --- a/openapi/src/main/java/io/micronaut/openapi/visitor/OpenApiControllerVisitor.java +++ b/openapi/src/main/java/io/micronaut/openapi/visitor/OpenApiControllerVisitor.java @@ -230,7 +230,7 @@ protected String description(MethodElement element) { } @Override - protected List classTags(ClassElement element, VisitorContext context) { + protected List getUserDefinedClassTags(ClassElement element, VisitorContext context) { return readTags(additionalTags, context); } diff --git a/openapi/src/main/java/io/micronaut/openapi/visitor/OpenApiEndpointVisitor.java b/openapi/src/main/java/io/micronaut/openapi/visitor/OpenApiEndpointVisitor.java index 48a3f34e09..a8607f1c09 100644 --- a/openapi/src/main/java/io/micronaut/openapi/visitor/OpenApiEndpointVisitor.java +++ b/openapi/src/main/java/io/micronaut/openapi/visitor/OpenApiEndpointVisitor.java @@ -221,7 +221,7 @@ protected String description(MethodElement element) { } @Override - protected List classTags(ClassElement element, VisitorContext context) { + protected List getUserDefinedClassTags(ClassElement element, VisitorContext context) { var allTags = new ArrayList<>(tags); allTags.addAll(ContextUtils.get(MICRONAUT_INTERNAL_OPENAPI_ENDPOINT_CLASS_TAGS, TAGS_LIST_ARGUMENT, Collections.emptyList(), context)); return allTags;