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

Minor code improvements. Code format #1777

Merged
merged 1 commit into from
Sep 30, 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 @@ -15,15 +15,15 @@
*/
package io.micronaut.openapi.annotation.mappers;

import java.util.Collections;
import java.util.List;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.inject.annotation.TypedAnnotationMapper;
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.openapi.annotation.OpenAPIInclude;
import io.micronaut.openapi.annotation.OpenAPIManagement;

import java.util.Collections;
import java.util.List;

import static io.micronaut.openapi.visitor.ElementUtils.EMPTY_ANNOTATION_VALUES_ARRAY;
import static io.micronaut.openapi.visitor.OpenApiModelProp.PROP_SECURITY;
import static io.micronaut.openapi.visitor.OpenApiModelProp.PROP_TAGS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package io.micronaut.openapi.annotation.mappers;

import java.util.Collections;
import java.util.List;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.inject.annotation.TypedAnnotationMapper;
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.openapi.annotation.OpenAPIInclude;
import io.micronaut.openapi.annotation.OpenAPISecurity;

import java.util.Collections;
import java.util.List;

import static io.micronaut.openapi.visitor.ElementUtils.EMPTY_ANNOTATION_VALUES_ARRAY;
import static io.micronaut.openapi.visitor.OpenApiModelProp.PROP_SECURITY;
import static io.micronaut.openapi.visitor.OpenApiModelProp.PROP_TAGS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package io.micronaut.openapi.annotation.transformers;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.inject.annotation.TypedAnnotationTransformer;
import io.micronaut.inject.visitor.VisitorContext;

import java.lang.annotation.Annotation;
import java.lang.annotation.RetentionPolicy;
import java.util.Collections;
import java.util.List;

import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.inject.annotation.TypedAnnotationTransformer;
import io.micronaut.inject.visitor.VisitorContext;

/**
* Changes the Retention Policy of the annotation to SOURCE.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package io.micronaut.openapi.javadoc;

import io.micronaut.core.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;

import io.micronaut.core.annotation.Nullable;

/**
* A parsed javadoc description.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@
*/
package io.micronaut.openapi.javadoc;

import java.util.Set;

import io.micronaut.core.util.CollectionUtils;

import com.github.chhorz.javadoc.JavaDoc;
import com.github.chhorz.javadoc.JavaDocParserBuilder;
import com.github.chhorz.javadoc.OutputType;
import com.github.chhorz.javadoc.tags.ParamTag;
import com.github.chhorz.javadoc.tags.PropertyTag;
import com.github.chhorz.javadoc.tags.ReturnTag;
import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter;
import io.micronaut.core.util.CollectionUtils;

import java.util.Set;

/**
* Very simple javadoc parser that can used to parse out the first paragraph description and parameter / return descriptions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
package io.micronaut.openapi.postprocessors;

import java.util.ArrayList;
import java.util.List;

import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.util.StringUtils;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;

import java.util.ArrayList;
import java.util.List;

import static io.micronaut.openapi.visitor.SchemaUtils.setSpecVersion;
import static io.swagger.v3.oas.models.Components.COMPONENTS_SCHEMAS_REF;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ protected void addAttribute(Map.Entry<String, String> entry) {
*/
protected String toHtmlAttributes() {
return options.entrySet().stream()
.map(e -> e.getKey() + "=\"" + e.getValue() + '"')
.collect(Collectors.joining(" "));
.map(e -> e.getKey() + "=\"" + e.getValue() + '"')
.collect(Collectors.joining(" "));
}

protected String getFinalUrlPrefix(OpenApiViewConfig.RendererType rendererType, VisitorContext context) {
Expand Down Expand Up @@ -246,8 +246,11 @@ static <T extends AbstractViewConfig> T fromProperties(T cfg, Map<String, Object
}

cfg.options.putAll(defaultOptions);
properties.entrySet().stream().filter(entry -> entry.getKey().startsWith(cfg.prefix))
.forEach(cfg::addAttribute);
for (var entry : properties.entrySet()) {
if (entry.getKey().startsWith(cfg.prefix)) {
cfg.addAttribute(entry);
}
}
return cfg;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@
*/
package io.micronaut.openapi.view;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.StringUtils;
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.openapi.view.OpenApiViewConfig.RendererType;
import io.micronaut.openapi.visitor.Pair;
import io.micronaut.openapi.visitor.group.OpenApiInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

import static io.micronaut.openapi.visitor.StringUtil.SLASH;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
*/
package io.micronaut.openapi.view;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.StringUtils;
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.openapi.view.OpenApiViewConfig.RendererType;
import io.micronaut.openapi.visitor.Pair;
import io.micronaut.openapi.visitor.group.OpenApiInfo;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;

import static io.micronaut.openapi.visitor.StringUtil.SLASH;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ final class SwaggerUIConfig extends AbstractViewConfig {
private static final String OPTION_OAUTH2 = "oauth2";
private static final String PREFIX_SWAGGER_UI = "swagger-ui";
private static final String KEY_VALUE_SEPARATOR = ": ";
private static final String COMMNA_NEW_LINE = ",\n";
private static final String COMMA_NEW_LINE = ",\n";

// https://github.com/swagger-api/swagger-ui/blob/HEAD/docs/usage/configuration.md
private static final Map<String, Function<String, Object>> VALID_OPTIONS = new HashMap<>(30);
Expand Down Expand Up @@ -218,7 +218,7 @@ private String toOptions(@NonNull Map<String, Function<String, Object>> validOpt
.sorted(Map.Entry.comparingByKey())
.map(e -> ((keyPrefix != null && e.getKey().startsWith(keyPrefix)) ? e.getKey().substring(keyPrefix.length()) : e.getKey())
+ KEY_VALUE_SEPARATOR + e.getValue())
.collect(Collectors.joining(COMMNA_NEW_LINE));
.collect(Collectors.joining(COMMA_NEW_LINE));
}

@NonNull
Expand Down Expand Up @@ -271,7 +271,7 @@ public String render(String template, @Nullable VisitorContext context) {
template = replacePlaceHolder(template, PREFIX_SWAGGER_UI + ".attributes", toOptions(), EMPTY_STRING);

if (theme != null && Theme.CLASSIC != theme) {
var themeCssLink = isDefaultThemeUrl ? finalUrlPrefix + theme.getCss() + ".css" : themeUrl;
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,11 @@ private void processTags(ClassElement element, VisitorContext context) {
}

private void processExternalDocs(ClassElement element, VisitorContext context) {
var externalDocsAnn = element.findAnnotation(io.swagger.v3.oas.annotations.ExternalDocumentation.class);
classExternalDocs = externalDocsAnn
.flatMap(o -> toValue(o.getValues(), context, ExternalDocumentation.class, null))
.orElse(null);
var externalDocsAnn = element.findAnnotation(io.swagger.v3.oas.annotations.ExternalDocumentation.class).orElse(null);
if (externalDocsAnn == null) {
return;
}
classExternalDocs = toValue(externalDocsAnn.getValues(), context, ExternalDocumentation.class, null);
}

private boolean containsTag(String name, List<Tag> tags) {
Expand Down Expand Up @@ -525,8 +526,8 @@ public void visitMethod(MethodElement element, VisitorContext context) {
}

private void addParamsByUriTemplate(String path, Map<String, UriMatchVariable> pathVariables,
Map<String, UriMatchVariable> queryParams,
Operation operation) {
Map<String, UriMatchVariable> queryParams,
Operation operation) {

// check path variables in URL template which do not map to method parameters
for (var entry : pathVariables.entrySet()) {
Expand Down Expand Up @@ -1371,7 +1372,7 @@ private Pair<String, PathItem> readWebhook(@Nullable AnnotationValue<Webhook> we
Operation operation;
HttpMethod method;
if (operationAnn != null) {
operation = toValue(operationAnn.getValues(), context, Operation.class, null).orElse(null);
operation = toValue(operationAnn.getValues(), context, Operation.class, null);
method = HttpMethod.parse(operationAnn.stringValue(PROP_METHOD).orElse(httpMethod.name()));
} else {
operation = new Operation();
Expand All @@ -1384,20 +1385,21 @@ private Pair<String, PathItem> readWebhook(@Nullable AnnotationValue<Webhook> we

private Map<PathItem, Operation> readOperations(String path, HttpMethod httpMethod, List<PathItem> pathItems, MethodElement element, VisitorContext context, @Nullable ClassElement jsonViewClass) {
var swaggerOperations = new HashMap<PathItem, Operation>(pathItems.size());
var operationAnn = element.findAnnotation(io.swagger.v3.oas.annotations.Operation.class);
var operationAnn = element.findAnnotation(io.swagger.v3.oas.annotations.Operation.class).orElse(null);

for (PathItem pathItem : pathItems) {
var swaggerOperation = operationAnn
.flatMap(o -> toValue(o.getValues(), context, Operation.class, jsonViewClass))
.orElse(new Operation());
var swaggerOperation = operationAnn != null ? toValue(operationAnn.getValues(), context, Operation.class, jsonViewClass) : null;
if (swaggerOperation == null) {
swaggerOperation = new Operation();
}

if (CollectionUtils.isNotEmpty(swaggerOperation.getParameters())) {
swaggerOperation.getParameters().removeIf(Objects::isNull);
}

ParameterElement[] methodParams = element.getParameters();
if (ArrayUtils.isNotEmpty(methodParams) && operationAnn.isPresent()) {
var paramAnns = operationAnn.get().getAnnotations(PROP_PARAMETERS, io.swagger.v3.oas.annotations.Parameter.class);
if (ArrayUtils.isNotEmpty(methodParams) && operationAnn != null) {
var paramAnns = operationAnn.getAnnotations(PROP_PARAMETERS, io.swagger.v3.oas.annotations.Parameter.class);
if (CollectionUtils.isNotEmpty(paramAnns)) {
for (ParameterElement methodParam : methodParams) {
AnnotationValue<io.swagger.v3.oas.annotations.Parameter> paramAnn = null;
Expand Down Expand Up @@ -1550,11 +1552,11 @@ private Parameter.StyleEnum paramStyle(ParameterStyle paramAnnStyle) {
}

private ExternalDocumentation readExternalDocs(MethodElement element, VisitorContext context) {
var externalDocsAnn = element.findAnnotation(io.swagger.v3.oas.annotations.ExternalDocumentation.class);

return externalDocsAnn
.flatMap(o -> toValue(o.getValues(), context, ExternalDocumentation.class, null))
.orElse(null);
var externalDocsAnn = element.findAnnotation(io.swagger.v3.oas.annotations.ExternalDocumentation.class).orElse(null);
if (externalDocsAnn == null) {
return null;
}
return toValue(externalDocsAnn.getValues(), context, ExternalDocumentation.class, null);
}

private void readSecurityRequirements(MethodElement element, String path, Operation operation, VisitorContext context) {
Expand Down Expand Up @@ -1727,9 +1729,8 @@ private void processResponses(Operation operation,
if (apiResponses.containsKey(responseCode)) {
continue;
}
Optional<ApiResponse> newResponse = toValue(responseAnn.getValues(), context, ApiResponse.class, jsonViewClass);
if (newResponse.isPresent()) {
ApiResponse newApiResponse = newResponse.get();
ApiResponse newApiResponse = toValue(responseAnn.getValues(), context, ApiResponse.class, jsonViewClass);
if (newApiResponse != null) {
if (responseAnn.booleanValue("useReturnTypeSchema").orElse(false) && element != null) {
addResponseContent(element, context, Utils.resolveOpenApi(context), newApiResponse, jsonViewClass);
} else {
Expand Down Expand Up @@ -1797,7 +1798,7 @@ private Pair<RequestBody, Boolean> readSwaggerRequestBody(Element element, List<

var jsonViewClass = element instanceof ParameterElement ? getJsonViewClass(element, context) : null;

RequestBody requestBody = toValue(requestBodyAnn.getValues(), context, RequestBody.class, jsonViewClass).orElse(null);
RequestBody requestBody = toValue(requestBodyAnn.getValues(), context, RequestBody.class, jsonViewClass);
// if media type doesn't set in swagger annotation, check micronaut annotation
if (contentAnn != null
&& contentAnn.stringValue(PROP_MEDIA_TYPE).isEmpty()
Expand Down Expand Up @@ -1868,9 +1869,14 @@ private void processUrlCallbackExpression(VisitorContext context,
var pathItem = new PathItem();
if (CollectionUtils.isNotEmpty(operationAnns)) {
for (var operationAnn : operationAnns) {
final Optional<HttpMethod> operationMethod = operationAnn.get(PROP_METHOD, HttpMethod.class);
operationMethod.ifPresent(httpMethod -> toValue(operationAnn.getValues(), context, Operation.class, jsonViewClass)
.ifPresent(op -> setOperationOnPathItem(pathItem, httpMethod, op)));
HttpMethod httpMethod = operationAnn.get(PROP_METHOD, HttpMethod.class).orElse(null);
if (httpMethod == null) {
continue;
}
var op = toValue(operationAnn.getValues(), context, Operation.class, jsonViewClass);
if (op != null) {
setOperationOnPathItem(pathItem, httpMethod, op);
}
}
}
Map<String, Callback> callbacks = initCallbacks(operation);
Expand Down Expand Up @@ -2103,8 +2109,10 @@ private List<Tag> readTags(ClassElement element, VisitorContext context) {
final List<Tag> readTags(List<AnnotationValue<io.swagger.v3.oas.annotations.tags.Tag>> tagAnns, VisitorContext context) {
var tags = new ArrayList<Tag>();
for (var tagAnn : tagAnns) {
toValue(tagAnn.getValues(), context, Tag.class, null)
.ifPresent(tags::add);
var tag = toValue(tagAnn.getValues(), context, Tag.class, null);
if (tag != null) {
tags.add(tag);
}
}
return tags;
}
Expand Down
Loading
Loading