Skip to content

Commit 6ddfb17

Browse files
committed
fix(api): address Copilot review issues in Hubble 2.0 API adaptation
- SchemaTemplateAPI: add ensurePdModeEnabled() guard to all endpoints to prevent NPE when MetaManager is not initialized in standalone mode - SchemaTemplateAPI: add null body check in create() and update() to return 400 instead of NPE/500 on missing request body - GraphsAPI.listProfile: format default_update_time consistently with create_time using DATE_FORMATTER instead of raw Date serialization - GraphsAPI.manage: guard value.getClass() against null action value to prevent NPE when building the validation error message - GraphSpaceAPI.deleteDefaultRole: wrap HugeDefaultRole.valueOf() in try/catch to return 400 for invalid role values instead of 500 - GraphManager: add isPDEnabled() guard to all schema-template methods to prevent NPE when MetaManager is not available in standalone mode - GraphManager.updateGraphNickname: propagate PD persistence failure to caller with in-memory rollback, preventing silent state inconsistency
1 parent f868dbc commit 6ddfb17

4 files changed

Lines changed: 47 additions & 6 deletions

File tree

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/profile/GraphsAPI.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,13 @@ public Object listProfile(@Context GraphManager manager,
184184
boolean isDefault = defaultGraphs.containsKey(graph);
185185
profile.put("default", isDefault);
186186
if (isDefault) {
187-
profile.put("default_update_time", defaultGraphs.get(graph));
187+
Date defaultUpdateTime = defaultGraphs.get(graph);
188+
if (defaultUpdateTime != null) {
189+
LocalDateTime ldt = defaultUpdateTime.toInstant()
190+
.atZone(ZoneId.systemDefault()).toLocalDateTime();
191+
profile.put("default_update_time",
192+
DATE_FORMATTER.format(ldt));
193+
}
188194
}
189195

190196
Date createTime = hg.createTime();
@@ -342,7 +348,7 @@ public Map<String, String> manage(@Context GraphManager manager,
342348
Object value = actionMap.get(GRAPH_ACTION);
343349
E.checkArgument(value instanceof String,
344350
"Invalid action type '%s', must be string",
345-
value.getClass());
351+
value == null ? "null" : value.getClass().getSimpleName());
346352
String action = (String) value;
347353
switch (action) {
348354
case UPDATE:

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/space/GraphSpaceAPI.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,13 @@ public void deleteDefaultRole(@Context GraphManager manager,
231231
throw new ForbiddenException("Forbidden to delete role " + role);
232232
}
233233

234-
HugeDefaultRole defaultRole =
235-
HugeDefaultRole.valueOf(role.toUpperCase());
234+
HugeDefaultRole defaultRole;
235+
try {
236+
defaultRole = HugeDefaultRole.valueOf(role.toUpperCase());
237+
} catch (IllegalArgumentException e) {
238+
E.checkArgument(false, "Invalid role value '%s'", role);
239+
defaultRole = null; // unreachable, satisfies compiler
240+
}
236241
boolean hasGraph = defaultRole.equals(HugeDefaultRole.OBSERVER);
237242
E.checkArgument(!hasGraph || StringUtils.isNotEmpty(graph),
238243
"Must set a graph for observer");

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/space/SchemaTemplateAPI.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public class SchemaTemplateAPI extends API {
6666
public Object list(@Context GraphManager manager,
6767
@PathParam("graphspace") String graphSpace) {
6868
LOG.debug("List all schema templates for graph space {}", graphSpace);
69+
ensurePdModeEnabled(manager);
6970

7071
Set<String> templates = manager.schemaTemplates(graphSpace);
7172
return ImmutableMap.of("schema_templates", templates);
@@ -81,6 +82,7 @@ public Object get(@Context GraphManager manager,
8182
@PathParam("name") String name) {
8283
LOG.debug("Get schema template by name '{}' for graph space {}",
8384
name, graphSpace);
85+
ensurePdModeEnabled(manager);
8486

8587
return manager.serializer().writeSchemaTemplate(
8688
schemaTemplate(manager, graphSpace, name));
@@ -97,6 +99,9 @@ public String create(@Context GraphManager manager,
9799
JsonSchemaTemplate jsonSchemaTemplate) {
98100
LOG.debug("Create schema template {} for graph space: '{}'",
99101
jsonSchemaTemplate, graphSpace);
102+
ensurePdModeEnabled(manager);
103+
E.checkArgumentNotNull(jsonSchemaTemplate,
104+
"Request body cannot be null or empty");
100105
jsonSchemaTemplate.checkCreate(false);
101106

102107
E.checkArgument(manager.graphSpace(graphSpace) != null,
@@ -120,6 +125,7 @@ public void delete(@Context GraphManager manager,
120125
@Context SecurityContext sc) {
121126
LOG.debug("Remove schema template by name '{}' for graph space '{}'",
122127
name, graphSpace);
128+
ensurePdModeEnabled(manager);
123129
E.checkArgument(manager.graphSpace(graphSpace) != null,
124130
"The graph space '%s' is not exist", graphSpace);
125131

@@ -148,6 +154,9 @@ public String update(@Context GraphManager manager,
148154
@PathParam("name") String name,
149155
@Context SecurityContext sc,
150156
JsonSchemaTemplate jsonSchemaTemplate) {
157+
ensurePdModeEnabled(manager);
158+
E.checkArgumentNotNull(jsonSchemaTemplate,
159+
"Request body cannot be null or empty");
151160
jsonSchemaTemplate.checkUpdate();
152161

153162
SchemaTemplate old = schemaTemplate(manager, graphSpace, name);

hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/core/GraphManager.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2226,8 +2226,13 @@ public void updateGraphNickname(String graphSpace, String graphName,
22262226
this.metaManager.notifyGraphUpdate(graphSpace, graphName);
22272227
}
22282228
} catch (Exception e) {
2229-
LOG.warn("Failed to persist nickname for graph '{}/{}': {}",
2230-
graphSpace, graphName, e.getMessage());
2229+
// Roll back the in-memory change so that the runtime state stays
2230+
// consistent with what was actually persisted.
2231+
if (g != null) {
2232+
g.nickname(null);
2233+
}
2234+
throw new HugeException("Failed to persist nickname for graph " +
2235+
"'%s/%s'", e, graphSpace, graphName);
22312236
}
22322237
}
22332238

@@ -2240,19 +2245,35 @@ public String cluster() {
22402245
}
22412246

22422247
public Set<String> schemaTemplates(String graphSpace) {
2248+
if (!isPDEnabled()) {
2249+
throw new HugeException("Schema templates are not supported in " +
2250+
"standalone (non-PD) mode");
2251+
}
22432252
return this.metaManager.schemaTemplates(graphSpace);
22442253
}
22452254

22462255
public void createSchemaTemplate(String graphSpace, SchemaTemplate template) {
2256+
if (!isPDEnabled()) {
2257+
throw new HugeException("Schema templates are not supported in " +
2258+
"standalone (non-PD) mode");
2259+
}
22472260
checkSchemaTemplateName(template.name());
22482261
this.metaManager.addSchemaTemplate(graphSpace, template);
22492262
}
22502263

22512264
public void dropSchemaTemplate(String graphSpace, String name) {
2265+
if (!isPDEnabled()) {
2266+
throw new HugeException("Schema templates are not supported in " +
2267+
"standalone (non-PD) mode");
2268+
}
22522269
this.metaManager.removeSchemaTemplate(graphSpace, name);
22532270
}
22542271

22552272
public void updateSchemaTemplate(String graphSpace, SchemaTemplate template) {
2273+
if (!isPDEnabled()) {
2274+
throw new HugeException("Schema templates are not supported in " +
2275+
"standalone (non-PD) mode");
2276+
}
22562277
this.metaManager.updateSchemaTemplate(graphSpace, template);
22572278
}
22582279

0 commit comments

Comments
 (0)