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: guard nullpointer exception with if statement #740

Merged
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 @@ -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