Skip to content

Commit

Permalink
Fix sonar errors
Browse files Browse the repository at this point in the history
  • Loading branch information
altro3 committed Mar 21, 2024
1 parent 1a5df68 commit 0be2c90
Show file tree
Hide file tree
Showing 7 changed files with 281 additions and 275 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.media.Content
import io.swagger.v3.oas.annotations.media.Schema
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.responses.ApiResponses
import io.swagger.v3.oas.annotations.tags.Tag

// end::imports[]
Expand All @@ -23,13 +22,9 @@ open class HelloController {
*/
@Get(uri = "/greetings/{name}", produces = [MediaType.TEXT_PLAIN])
@Operation(summary = "Greets a person", description = "A friendly greeting is returned")
// Please Note: Repeatable Annotations with non-SOURCE retentions are not yet supported with Kotlin, so we are using `@ApiResponses`
// instead of `@ApiResponse`, see https://youtrack.jetbrains.com/issue/KT-12794
@ApiResponses(
ApiResponse(content = [Content(mediaType = "text/plain", schema = Schema(type = "string"))]),
ApiResponse(responseCode = "400", description = "Invalid Name Supplied"),
ApiResponse(responseCode = "404", description = "Person not found")
)
@ApiResponse(content = [Content(mediaType = "text/plain", schema = Schema(type = "string"))])
@ApiResponse(responseCode = "400", description = "Invalid Name Supplied")
@ApiResponse(responseCode = "404", description = "Person not found")
@Tag(name = "greeting")
open fun greetings(name: String): Mono<String> {
return Mono.just("Hello $name, how are you doing?")
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ protected Schema<?> bindSchemaForElement(VisitorContext context, TypedElement el
}

// Schema finalSchemaToBind = schemaToBind;
processJavaxValidationAnnotations(element, elementType, schemaToBind);
processJakartaValidationAnnotations(element, elementType, schemaToBind);

final ComposedSchema composedSchema;
final Schema<?> topLevelSchema;
Expand Down Expand Up @@ -1397,7 +1397,7 @@ protected Schema<?> bindSchemaForElement(VisitorContext context, TypedElement el
return originalSchema;
}

protected void processJavaxValidationAnnotations(Element element, ClassElement elementType, Schema<?> schemaToBind) {
protected void processJakartaValidationAnnotations(Element element, ClassElement elementType, Schema<?> schemaToBind) {

final boolean isIterableOrMap = elementType.isIterable() || elementType.isAssignable(Map.class);

Expand Down Expand Up @@ -2094,7 +2094,7 @@ private void bindSchemaIfNecessary(VisitorContext context, AnnotationValue<?> av
}
if (not.isPresent()) {
final Schema<?> schemaNot = resolveSchema(null, ContextUtils.getClassElement(not.get(), context), context, Collections.emptyList(), jsonViewClass);
Map<CharSequence, Object> schemaMap = new HashMap<>();
var schemaMap = new HashMap<CharSequence, Object>();
schemaToValueMap(schemaMap, schemaNot);
valueMap.put("not", schemaMap);
}
Expand Down Expand Up @@ -2310,7 +2310,7 @@ private Schema<?> processSuperTypes(Schema<?> schema,
VisitorContext context,
@Nullable ClassElement jsonViewClass) {

if (type.getName().equals(Object.class.getName())) {
if (type == null || type.getName().equals(Object.class.getName())) {
return null;
}

Expand Down Expand Up @@ -2450,7 +2450,7 @@ protected Schema<?> readSchema(AnnotationValue<io.swagger.v3.oas.annotations.med
schema.setEnum(getEnumValues(enumEl, schema.getType(), schema.getFormat(), context));
}
} else {
JavadocDescription javadoc = Utils.getJavadocParser().parse(type.getDescription());
JavadocDescription javadoc = type != null ? Utils.getJavadocParser().parse(type.getDescription()) : null;
populateSchemaProperties(openAPI, context, type, typeArgs, schema, mediaTypes, javadoc, jsonViewClass);
checkAllOf(schema);
}
Expand Down Expand Up @@ -2779,7 +2779,7 @@ private void processArgTypeAnnotations(ClassElement type, @Nullable Schema<?> sc
if (isNullable(type) && !isNotNullable(type)) {
SchemaUtils.setNullable(schema);
}
processJavaxValidationAnnotations(type, type, schema);
processJakartaValidationAnnotations(type, type, schema);
}

private Schema<?> getPrimitiveType(ClassElement type, String typeName) {
Expand Down Expand Up @@ -2837,12 +2837,14 @@ protected <T, A extends Annotation> List<T> processOpenApiAnnotation(Element ele
}
for (AnnotationValue<A> tag : annotations) {
Map<CharSequence, Object> values;
if (tag.getAnnotationName().equals(io.swagger.v3.oas.annotations.security.SecurityRequirement.class.getName()) && !tag.getValues().isEmpty()) {
Object name = tag.getValues().get("name");
Object scopes = Optional.ofNullable(tag.getValues().get("scopes")).orElse(new ArrayList<String>());
var tagValues = tag.getValues();
if (tag.getAnnotationName().equals(io.swagger.v3.oas.annotations.security.SecurityRequirement.class.getName())
&& !tagValues.isEmpty()) {
Object name = tagValues.get("name");
Object scopes = Optional.ofNullable(tagValues.get("scopes")).orElse(new ArrayList<String>());
values = Collections.singletonMap((CharSequence) name, scopes);
} else {
values = tag.getValues();
values = tagValues;
}
Optional<T> tagOpt = toValue(values, context, modelType, null);
if (tagOpt.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Future;

import com.fasterxml.jackson.annotation.JsonView;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.Internal;
Expand All @@ -48,6 +49,8 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

import static io.micronaut.openapi.visitor.ConfigUtils.isJsonViewEnabled;

/**
* Some util methods.
*
Expand Down Expand Up @@ -395,4 +398,18 @@ private static MethodElement getCreatorConstructor(ClassElement classEl) {
Utils.getCreatorConstructorsCache().put(classEl.getName(), creatorConstructor);
return creatorConstructor;
}

public static ClassElement getJsonViewClass(Element element, VisitorContext context) {
if (!isJsonViewEnabled(context)) {
return null;
}
var jsonViewAnn = element.findAnnotation(JsonView.class).orElse(null);
if (jsonViewAnn != null) {
String jsonViewClassName = jsonViewAnn.stringValue().orElse(null);
if (jsonViewClassName != null) {
return ContextUtils.getClassElement(jsonViewClassName, context);
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,8 @@ private Map<Pair<String, String>, OpenApiInfo> divideOpenapiByGroupsAndVersions(
continue;
}
for (EndpointGroupInfo endpointGroupInfo : endpointInfo.getGroups().values()) {
if (CollectionUtils.isNotEmpty(endpointInfo.getExcludedGroups()) && endpointInfo.getExcludedGroups().contains(endpointGroupInfo)) {
if (CollectionUtils.isNotEmpty(endpointInfo.getExcludedGroups())
&& endpointInfo.getExcludedGroups().contains(endpointGroupInfo.getName())) {
continue;
}
OpenAPI newOpenApi = addOpenApiInfo(endpointGroupInfo.getName(), endpointInfo.getVersion(), openApi, result, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.micronaut.openapi.AbstractOpenApiTypeElementSpec
import io.swagger.v3.oas.models.OpenAPI
import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.media.Schema
import io.swagger.v3.oas.models.parameters.Parameter
import spock.lang.Issue

import java.time.OffsetDateTime
Expand Down Expand Up @@ -1161,7 +1162,7 @@ public class MyBean {}
buildBeanDefinition("test.MyBean", '''
package test;
import jakarta.validation.constraints.NegativeOrZero;
import io.swagger.v3.oas.annotations.enums.ParameterStyle;import jakarta.validation.constraints.NegativeOrZero;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.PositiveOrZero;
Expand All @@ -1185,11 +1186,13 @@ class PersonController {
@Parameter(
name = "uuid",
description = "The identifier",
style = ParameterStyle.LABEL,
schema = @Schema(implementation = String.class),
in = ParameterIn.PATH
),
@Parameter(
name = "userUuid",
style = ParameterStyle.DEEPOBJECT,
description = "The user identifier",
schema = @Schema(implementation = String.class),
in = ParameterIn.PATH
Expand Down Expand Up @@ -1220,13 +1223,15 @@ public class MyBean {}
op.parameters.get(0).name == 'uuid'
op.parameters.get(0).schema.type == 'string'
op.parameters.get(0).in == 'path'
op.parameters.get(0).style == Parameter.StyleEnum.LABEL
op.parameters.get(0).description == 'The identifier'
op.parameters.get(0).example == null
op.parameters.get(0).$ref == null

op.parameters.get(1).name == 'userUuid'
op.parameters.get(1).schema.type == 'string'
op.parameters.get(1).in == 'path'
op.parameters.get(1).style == Parameter.StyleEnum.DEEPOBJECT
op.parameters.get(1).description == 'The user identifier'
op.parameters.get(1).example == null
op.parameters.get(1).$ref == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ import java.time.Instant;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Post;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.extensions.Extension;import io.swagger.v3.oas.annotations.extensions.ExtensionProperty;import io.swagger.v3.oas.annotations.media.Schema;
@Controller
class OpenApiController {
Expand Down Expand Up @@ -211,13 +211,19 @@ class MyDto {
minimum = "5",
maximum = "20",
accessMode = Schema.AccessMode.READ_ONLY,
defaultValue = "{\\"stampWidth\\": 100}",
defaultValue = """
{"stampWidth": 100}""",
required = true,
example = "{\\n" +
" \\"stampWidth\\": 220,\\n" +
" \\"stampHeight\\": 85,\\n" +
" \\"pageNumber\\": 1\\n" +
"}"
example = """
{
"stampWidth": 220,
"stampHeight": 85,
"pageNumber": 1
}""",
extensions = {
@Extension(name = "ext1", properties = @ExtensionProperty(name = "prop11", value = "val11")),
@Extension(name = "ext2", properties = @ExtensionProperty(name = "prop21", value = "val21")),
}
)
class Parameters {
Expand Down Expand Up @@ -281,16 +287,24 @@ class MyBean {}

dtoSchema.properties.parameters.get$ref() == "#/components/schemas/ParametersSchema"
!openAPI.components.schemas.MyDto.required
openAPI.components.schemas.ParametersSchema.deprecated
openAPI.components.schemas.ParametersSchema.nullable
openAPI.components.schemas.ParametersSchema.readOnly
openAPI.components.schemas.ParametersSchema.description == 'this is description'
openAPI.components.schemas.ParametersSchema.example
openAPI.components.schemas.ParametersSchema.example.stampWidth == 220
openAPI.components.schemas.ParametersSchema.example.stampHeight == 85
openAPI.components.schemas.ParametersSchema.example.pageNumber == 1
openAPI.components.schemas.ParametersSchema.default
openAPI.components.schemas.ParametersSchema.default.stampWidth == 100

def schema = openAPI.components.schemas.ParametersSchema

schema.deprecated
schema.nullable
schema.readOnly
schema.description == 'this is description'
schema.example
schema.example.stampWidth == 220
schema.example.stampHeight == 85
schema.example.pageNumber == 1
schema.default
schema.default.stampWidth == 100

schema.extensions.'x-ext1'
schema.extensions.'x-ext1'.prop11 == 'val11'
schema.extensions.'x-ext2'
schema.extensions.'x-ext2'.prop21 == 'val21'
}

void "test schema on property level with default values"() {
Expand Down

0 comments on commit 0be2c90

Please sign in to comment.