Skip to content

Commit

Permalink
fix: guard nullpointer exception with if statement
Browse files Browse the repository at this point in the history
  • Loading branch information
timonback committed May 3, 2024
1 parent 9e7a4be commit 060a016
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageObject;
import io.github.springwolf.asyncapi.v3.model.channel.message.MessageReference;
import io.github.springwolf.asyncapi.v3.model.schema.SchemaObject;
import jakarta.annotation.Nullable;

import java.util.Map;

public interface ComponentsService {

Map<String, SchemaObject> getSchemas();

@Nullable
SchemaObject resolveSchema(String schemaName);

String registerSchema(SchemaObject headers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
import io.swagger.v3.oas.models.media.ObjectSchema;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.media.StringSchema;
import jakarta.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -62,7 +60,6 @@ public Map<String, SchemaObject> getSchemas() {
}

@Override
@Nullable
public SchemaObject resolveSchema(String schemaName) {
if (schemas.containsKey(schemaName)) {
return swaggerSchemaUtil.mapSchema(schemas.get(schemaName));
Expand Down Expand Up @@ -126,13 +123,13 @@ private String getSchemaName(Class<?> type, Map<String, Schema> schemas) {
}

if (schemas.size() == 1) {
return new ArrayList<>(schemas.keySet()).get(0);
return schemas.keySet().stream().findFirst().get();
}

Set<String> resolvedPayloadModelName =
runWithFqnSetting((unused) -> converter.read(type).keySet());
if (!resolvedPayloadModelName.isEmpty()) {
return new ArrayList<>(resolvedPayloadModelName).get(0);
return resolvedPayloadModelName.stream().findFirst().get();
}

return getNameFromClass(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ private NamedSchemaObject buildSchema(String contentType, Class<?> payloadType)
String componentsSchemaName = this.componentsService.registerSchema(payloadType, contentType);

SchemaObject schema = componentsService.resolveSchema(componentsSchemaName);
schema.setTitle(payloadType.getSimpleName());
if (schema != null) {
schema.setTitle(payloadType.getSimpleName());
}

return new NamedSchemaObject(componentsSchemaName, schema);
}
Expand Down

0 comments on commit 060a016

Please sign in to comment.