Skip to content

Commit cf85a01

Browse files
committed
Limit JSpecify migration to existing behavior
1 parent ac4abbf commit cf85a01

22 files changed

Lines changed: 117 additions & 158 deletions

‎src/main/java/graphql/analysis/MaxQueryComplexityInstrumentation.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ public InstrumentationContext<List<ValidationError>> beginValidation(Instrumenta
9595

9696
@Override
9797
public InstrumentationContext<ExecutionResult> beginExecuteOperation(InstrumentationExecuteOperationParameters instrumentationExecuteOperationParameters, @Nullable InstrumentationState rawState) {
98-
State state = ofState(assertNotNull(rawState, "Instrumentation state must be created before callbacks"));
98+
State state = ofState(rawState);
9999
QueryComplexityCalculator queryComplexityCalculator = newQueryComplexityCalculator(instrumentationExecuteOperationParameters.getExecutionContext());
100100
int totalComplexity = queryComplexityCalculator.calculate();
101101
if (totalComplexity > maxComplexity) {
102102
QueryComplexityInfo queryComplexityInfo = QueryComplexityInfo.newQueryComplexityInfo()
103103
.complexity(totalComplexity)
104-
.instrumentationValidationParameters(state.instrumentationValidationParameters.get())
104+
.instrumentationValidationParameters(assertNotNull(state, "Instrumentation state must be created before callbacks").instrumentationValidationParameters.get())
105105
.instrumentationExecuteOperationParameters(instrumentationExecuteOperationParameters)
106106
.build();
107107
boolean throwAbortException = maxQueryComplexityExceededFunction.apply(queryComplexityInfo);

‎src/main/java/graphql/analysis/values/ValueTraverser.java‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.jspecify.annotations.NullMarked;
2323
import org.jspecify.annotations.Nullable;
2424

25-
import java.util.ArrayList;
2625
import java.util.LinkedHashMap;
2726
import java.util.List;
2827
import java.util.Map;
@@ -31,7 +30,6 @@
3130
import static graphql.Assert.assertShouldNeverHappen;
3231
import static graphql.Assert.assertTrue;
3332
import static graphql.analysis.values.ValueVisitor.ABSENCE_SENTINEL;
34-
import static java.util.Collections.unmodifiableList;
3533

3634
/**
3735
* This class allows you to traverse a set of input values according to the type system and optional
@@ -281,29 +279,29 @@ public static DataFetchingEnvironment visitPreOrder(DataFetchingEnvironment envi
281279
}
282280
if (newList != null) {
283281
GraphQLInputType inputType = GraphQLTypeUtil.unwrapOneAs(listInputType);
284-
List<@Nullable Object> copiedList = null;
282+
ImmutableList.Builder<Object> copiedList = null;
285283
int i = 0;
286284
for (Object subValue : newList) {
287285
Object newValue = visitPreOrderImpl(subValue, inputType, containingElements, visitor);
288286
if (copiedList != null) {
289287
if (newValue != ABSENCE_SENTINEL) {
290-
copiedList.add(newValue);
288+
copiedList.add(assertNotNull(newValue, "list element must not be null"));
291289
}
292290
} else if (hasChanged(newValue, subValue)) {
293291
// go into copy mode because something has changed
294292
// copy previous values up to this point
295-
copiedList = new ArrayList<>(newList.size());
293+
copiedList = ImmutableList.builder();
296294
for (int j = 0; j < i; j++) {
297-
copiedList.add(newList.get(j));
295+
copiedList.add(assertNotNull(newList.get(j), "list element must not be null"));
298296
}
299297
if (newValue != ABSENCE_SENTINEL) {
300-
copiedList.add(newValue);
298+
copiedList.add(assertNotNull(newValue, "list element must not be null"));
301299
}
302300
}
303301
i++;
304302
}
305303
if (copiedList != null) {
306-
return unmodifiableList(copiedList);
304+
return copiedList.build();
307305
} else {
308306
return newList;
309307
}

‎src/main/java/graphql/introspection/Introspection.java‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ private static String printDefaultValue(InputValueWithState inputValueWithState,
353353
static {
354354
IntrospectionDataFetcher<?> argsDataFetcher = environment -> {
355355
GraphQLFieldDefinition fieldDef = Assert.assertNotNull(environment.<GraphQLFieldDefinition>getSource(), "introspection field source should not be null");
356-
boolean includeDeprecated = Assert.assertNotNull(environment.<Boolean>getArgument("includeDeprecated"), "includeDeprecated must have a coerced value");
356+
Boolean includeDeprecated = environment.getArgument("includeDeprecated");
357357
return ImmutableKit.filter(fieldDef.getArguments(),
358-
arg -> includeDeprecated || !arg.isDeprecated());
358+
arg -> Assert.assertNotNull(includeDeprecated, "includeDeprecated must have a coerced value") || !arg.isDeprecated());
359359
};
360360
register(__Field, "name", nameDataFetcher);
361361
register(__Field, "description", descriptionDataFetcher);
@@ -394,13 +394,13 @@ private static String printDefaultValue(InputValueWithState inputValueWithState,
394394
Object type = environment.getSource();
395395
if (type instanceof GraphQLFieldsContainer) {
396396
GraphQLFieldsContainer fieldsContainer = (GraphQLFieldsContainer) type;
397-
boolean includeDeprecated = Assert.assertNotNull(environment.<Boolean>getArgument("includeDeprecated"), "includeDeprecated must have a coerced value");
397+
Boolean includeDeprecated = environment.getArgument("includeDeprecated");
398398
List<GraphQLFieldDefinition> fieldDefinitions = environment
399399
.getGraphQLSchema()
400400
.getCodeRegistry()
401401
.getFieldVisibility()
402402
.getFieldDefinitions(fieldsContainer);
403-
if (includeDeprecated) {
403+
if (Assert.assertNotNull(includeDeprecated, "includeDeprecated must have a coerced value")) {
404404
return fieldDefinitions;
405405
}
406406
return ImmutableKit.filter(fieldDefinitions,
@@ -435,9 +435,9 @@ private static String printDefaultValue(InputValueWithState inputValueWithState,
435435
private static final IntrospectionDataFetcher<?> enumValuesTypesFetcher = environment -> {
436436
Object type = environment.getSource();
437437
if (type instanceof GraphQLEnumType) {
438-
boolean includeDeprecated = Assert.assertNotNull(environment.<Boolean>getArgument("includeDeprecated"), "includeDeprecated must have a coerced value");
438+
Boolean includeDeprecated = environment.getArgument("includeDeprecated");
439439
List<GraphQLEnumValueDefinition> values = ((GraphQLEnumType) type).getValues();
440-
if (includeDeprecated) {
440+
if (Assert.assertNotNull(includeDeprecated, "includeDeprecated must have a coerced value")) {
441441
return values;
442442
}
443443
return ImmutableKit.filter(values,
@@ -449,13 +449,13 @@ private static String printDefaultValue(InputValueWithState inputValueWithState,
449449
private static final IntrospectionDataFetcher<?> inputFieldsFetcher = environment -> {
450450
Object type = environment.getSource();
451451
if (type instanceof GraphQLInputObjectType) {
452-
boolean includeDeprecated = Assert.assertNotNull(environment.<Boolean>getArgument("includeDeprecated"), "includeDeprecated must have a coerced value");
452+
Boolean includeDeprecated = environment.getArgument("includeDeprecated");
453453
List<GraphQLInputObjectField> inputFields = environment
454454
.getGraphQLSchema()
455455
.getCodeRegistry()
456456
.getFieldVisibility()
457457
.getFieldDefinitions((GraphQLInputObjectType) type);
458-
if (includeDeprecated) {
458+
if (Assert.assertNotNull(includeDeprecated, "includeDeprecated must have a coerced value")) {
459459
return inputFields;
460460
}
461461
return ImmutableKit.filter(inputFields,
@@ -651,9 +651,9 @@ public enum DirectiveLocation {
651651
};
652652
IntrospectionDataFetcher<?> argsDataFetcher = environment -> {
653653
GraphQLDirective directive = Assert.assertNotNull(environment.<GraphQLDirective>getSource(), "introspection directive source should not be null");
654-
boolean includeDeprecated = Assert.assertNotNull(environment.<Boolean>getArgument("includeDeprecated"), "includeDeprecated must have a coerced value");
654+
Boolean includeDeprecated = environment.getArgument("includeDeprecated");
655655
return ImmutableKit.filter(directive.getArguments(),
656-
arg -> includeDeprecated || !arg.isDeprecated());
656+
arg -> Assert.assertNotNull(includeDeprecated, "includeDeprecated must have a coerced value") || !arg.isDeprecated());
657657
};
658658
register(__Directive, "name", nameDataFetcher);
659659
register(__Directive, "description", descriptionDataFetcher);
@@ -706,9 +706,9 @@ public enum DirectiveLocation {
706706
register(__Schema, "queryType", GraphQLSchema.class, GraphQLSchema::getQueryType);
707707
register(__Schema, "mutationType", GraphQLSchema.class, GraphQLSchema::getMutationType);
708708
IntrospectionDataFetcher<?> directivesDataFetcher = environment -> {
709-
boolean includeDeprecated = Assert.assertNotNull(environment.<Boolean>getArgument("includeDeprecated"), "includeDeprecated must have a coerced value");
709+
Boolean includeDeprecated = environment.getArgument("includeDeprecated");
710710
return ImmutableKit.filter(environment.getGraphQLSchema().getDirectives(),
711-
directive -> includeDeprecated || !directive.isDeprecated());
711+
directive -> Assert.assertNotNull(includeDeprecated, "includeDeprecated must have a coerced value") || !directive.isDeprecated());
712712
};
713713
register(__Schema, "directives", directivesDataFetcher);
714714
register(__Schema, "subscriptionType", GraphQLSchema.class, GraphQLSchema::getSubscriptionType);

‎src/main/java/graphql/introspection/IntrospectionResultToSchema.java‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,13 @@ public class IntrospectionResultToSchema {
7676
*
7777
* @return a IDL Document of the schema
7878
*/
79+
@SuppressWarnings("unchecked")
7980
public Document createSchemaDefinition(Map<String, Object> introspectionResult) {
80-
Map<String, Object> schema = assertNotNull((Map<String, Object>) introspectionResult.get("__schema"), "__schema expected");
81+
Map<String, Object> schema = (Map<String, Object>) introspectionResult.get("__schema");
82+
assertNotNull(schema, "__schema expected");
8183

82-
Map<String, Object> queryType = assertNotNull((Map<String, Object>) schema.get("queryType"), "queryType expected");
84+
Map<String, Object> queryType = (Map<String, Object>) schema.get("queryType");
85+
assertNotNull(queryType, "queryType expected");
8386
TypeName query = TypeName.newTypeName().name(assertValidName((String) queryType.get("name"))).build();
8487
boolean nonDefaultQueryName = !"Query".equals(query.getName());
8588

@@ -167,12 +170,12 @@ private List<DirectiveLocation> createDirectiveLocations(List<Object> locations)
167170
}
168171

169172
private @Nullable TypeDefinition createTypeDefinition(Map<String, Object> type) {
170-
String kind = assertNotNull((String) type.get("kind"), "kind expected");
171-
String name = assertValidName((String) type.get("name"));
173+
String kind = (String) type.get("kind");
174+
String name = assertNotNull((String) type.get("name"), "name expected");
172175
if (name.startsWith("__")) {
173176
return null;
174177
}
175-
switch (kind) {
178+
switch (assertNotNull(kind, "kind expected")) {
176179
case "INTERFACE":
177180
return createInterface(type);
178181
case "OBJECT":
@@ -202,6 +205,7 @@ private List<DirectiveLocation> createDirectiveLocations(List<Object> locations)
202205
}
203206

204207

208+
@SuppressWarnings("unchecked")
205209
UnionTypeDefinition createUnion(Map<String, Object> input) {
206210
assertTrue("UNION".equals(input.get("kind")), "wrong input");
207211

@@ -219,6 +223,7 @@ UnionTypeDefinition createUnion(Map<String, Object> input) {
219223
return unionTypeDefinition.build();
220224
}
221225

226+
@SuppressWarnings("unchecked")
222227
EnumTypeDefinition createEnum(Map<String, Object> input) {
223228
assertTrue("ENUM".equals(input.get("kind")), "wrong input");
224229

@@ -240,15 +245,18 @@ EnumTypeDefinition createEnum(Map<String, Object> input) {
240245
return enumTypeDefinition.build();
241246
}
242247

248+
@SuppressWarnings("unchecked")
243249
InterfaceTypeDefinition createInterface(Map<String, Object> input) {
244250
assertTrue("INTERFACE".equals(input.get("kind")), "wrong input");
245251

246252
InterfaceTypeDefinition.Builder interfaceTypeDefinition = InterfaceTypeDefinition.newInterfaceTypeDefinition().name(assertValidName((String) input.get("name")));
247253
interfaceTypeDefinition.description(toDescription(input));
248-
List<Map<String, Object>> interfaces = (List<Map<String, Object>>) input.get("interfaces");
249-
if (interfaces != null) {
254+
if (input.containsKey("interfaces") && input.get("interfaces") != null) {
250255
interfaceTypeDefinition.implementz(
251-
map(interfaces, this::createTypeIndirection)
256+
map(
257+
(List<Map<String, Object>>) input.get("interfaces"),
258+
this::createTypeIndirection
259+
)
252260
);
253261
}
254262
List<Map<String, Object>> fields = assertNotNull((List<Map<String, Object>>) input.get("fields"), "fields expected");
@@ -258,6 +266,7 @@ InterfaceTypeDefinition createInterface(Map<String, Object> input) {
258266

259267
}
260268

269+
@SuppressWarnings("unchecked")
261270
InputObjectTypeDefinition createInputObject(Map<String, Object> input) {
262271
assertTrue("INPUT_OBJECT".equals(input.get("kind")), "wrong input");
263272

@@ -276,6 +285,7 @@ InputObjectTypeDefinition createInputObject(Map<String, Object> input) {
276285
return inputObjectTypeDefinition.build();
277286
}
278287

288+
@SuppressWarnings("unchecked")
279289
ObjectTypeDefinition createObject(Map<String, Object> input) {
280290
assertTrue("OBJECT".equals(input.get("kind")), "wrong input");
281291

@@ -323,6 +333,7 @@ private void createDeprecatedDirective(Map<String, Object> field, NodeDirectives
323333
}
324334
}
325335

336+
@SuppressWarnings("unchecked")
326337
private List<InputValueDefinition> createInputValueDefinitions(List<Map<String, Object>> args) {
327338
List<InputValueDefinition> result = new ArrayList<>(args.size());
328339
for (Map<String, Object> arg : args) {
@@ -341,6 +352,7 @@ private List<InputValueDefinition> createInputValueDefinitions(List<Map<String,
341352
return result;
342353
}
343354

355+
@SuppressWarnings("unchecked")
344356
private Type createTypeIndirection(Map<String, Object> type) {
345357
String kind = assertNotNull((String) type.get("kind"), "kind expected");
346358
switch (kind) {

‎src/main/java/graphql/introspection/IntrospectionWithDirectivesSupport.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ public GraphQLSchema apply(GraphQLSchema schema) {
134134
GraphQLTypeVisitorStub visitor = new GraphQLTypeVisitorStub() {
135135
@Override
136136
public TraversalControl visitGraphQLObjectType(GraphQLObjectType objectType, TraverserContext<GraphQLSchemaElement> context) {
137-
GraphQLCodeRegistry.Builder codeRegistry = assertNotNull(context.getVarFromParents(GraphQLCodeRegistry.Builder.class), "codeRegistry should not be null");
137+
GraphQLCodeRegistry.Builder codeRegistry = context.getVarFromParents(GraphQLCodeRegistry.Builder.class);
138138
// we need to change __XXX introspection types to have directive extensions
139139
if (INTROSPECTION_ELEMENTS.contains(objectType.getName())) {
140-
GraphQLObjectType newObjectType = addAppliedDirectives(objectType, codeRegistry, appliedDirectiveType, directiveArgumentType);
140+
GraphQLObjectType newObjectType = addAppliedDirectives(objectType, assertNotNull(codeRegistry, "codeRegistry should not be null"), appliedDirectiveType, directiveArgumentType);
141141
return changeNode(context, newObjectType);
142142
}
143143
return CONTINUE;

‎src/main/java/graphql/language/SchemaDefinition.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public List<OperationTypeDefinition> getOperationTypeDefinitions() {
6969
return operationTypeDefinitions;
7070
}
7171

72+
public @Nullable Description getDescription() {
73+
return description;
74+
}
75+
7276
@Override
7377
public List<Node> getChildren() {
7478
return FpKit.concat(directives.getDirectives(), operationTypeDefinitions);

‎src/main/java/graphql/normalized/ExecutableNormalizedField.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public boolean isConditional(GraphQLSchema schema) {
145145
}
146146

147147
for (GraphQLInterfaceType commonParentOutputInterface : parent.getInterfacesCommonToAllOutputTypes(schema)) {
148-
List<GraphQLObjectType> implementations = assertNotNull(schema.getImplementations(commonParentOutputInterface), "implementations should not be null");
148+
List<GraphQLObjectType> implementations = schema.getImplementations(commonParentOutputInterface);
149149
// __typename
150150
if (fieldName.equals(Introspection.TypeNameMetaFieldDef.getName()) && implementations.size() == objectTypeNames.size()) {
151151
return false;

‎src/main/java/graphql/normalized/ExecutableNormalizedOperation.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
@NullMarked
3131
public class ExecutableNormalizedOperation {
3232
private final OperationDefinition.Operation operation;
33-
private final @Nullable String operationName;
3433
private final Map<String, ImmutableList<QueryAppliedDirective>> operationDirectives;
34+
private final @Nullable String operationName;
3535
private final List<ExecutableNormalizedField> topLevelFields;
3636
private final ImmutableListMultimap<Field, ExecutableNormalizedField> fieldToNormalizedField;
3737
private final Map<ExecutableNormalizedField, MergedField> normalizedFieldToMergedField;

0 commit comments

Comments
 (0)