Skip to content

Commit cca8ba8

Browse files
authored
Fix useJavaBeansSemanticNaming and boolean field starts with is (apollographql#881)
Closes apollographql#815
1 parent 1ed831e commit cca8ba8

File tree

6 files changed

+70
-9
lines changed

6 files changed

+70
-9
lines changed

apollo-compiler/src/main/kotlin/com/apollographql/apollo/compiler/Util.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ fun String.escapeJavaReservedWord() = if (JAVA_RESERVED_WORDS.contains(this)) "$
1616

1717
fun String.toJavaBeansSemanticNaming(isBooleanField: Boolean): String {
1818
val prefix = if (isBooleanField) "is" else "get"
19+
if (isBooleanField && startsWith(prefix) && removePrefix(prefix) == removePrefix(prefix).capitalize()) {
20+
return this
21+
}
1922
return "$prefix${capitalize()}"
2023
}
2124

apollo-compiler/src/test/graphql/com/example/java_beans_semantic_naming/TestOperation.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ fragment HeroDetails on Character {
1818
pageInfo {
1919
hasNextPage
2020
}
21+
isEmpty
2122
}
2223
... on Droid {
2324
name

apollo-compiler/src/test/graphql/com/example/java_beans_semantic_naming/TestOperation.json

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@
5252
"fragmentsReferenced": [
5353
"HeroDetails"
5454
],
55-
"sourceWithFragments": "query TestQuery {\n hero {\n __typename\n name\n ...HeroDetails\n appearsIn\n }\n}\nfragment HeroDetails on Character {\n __typename\n name\n friendsConnection {\n __typename\n totalCount\n edges {\n __typename\n node {\n __typename\n name\n }\n }\n pageInfo {\n __typename\n hasNextPage\n }\n }\n ... on Droid {\n name\n primaryFunction\n }\n}",
56-
"operationId": "c0463bc20566eabd263da887f6c45205c7b42522fa28585baf0e9f6021a2a8a6"
55+
"sourceWithFragments": "query TestQuery {\n hero {\n __typename\n name\n ...HeroDetails\n appearsIn\n }\n}\nfragment HeroDetails on Character {\n __typename\n name\n friendsConnection {\n __typename\n totalCount\n edges {\n __typename\n node {\n __typename\n name\n }\n }\n pageInfo {\n __typename\n hasNextPage\n }\n isEmpty\n }\n ... on Droid {\n name\n primaryFunction\n }\n}",
56+
"operationId": "4141e194c5f3846dabfcb576e735c71968b03a940baf49cc5e647c5e50eda72a"
5757
}
5858
],
5959
"fragments": [
@@ -65,7 +65,7 @@
6565
],
6666
"fragmentName": "HeroDetails",
6767
"filePath": "src/test/graphql/com/example/java_beans_semantic_naming/TestOperation.graphql",
68-
"source": "fragment HeroDetails on Character {\n __typename\n name\n friendsConnection {\n __typename\n totalCount\n edges {\n __typename\n node {\n __typename\n name\n }\n }\n pageInfo {\n __typename\n hasNextPage\n }\n }\n ... on Droid {\n name\n primaryFunction\n }\n}",
68+
"source": "fragment HeroDetails on Character {\n __typename\n name\n friendsConnection {\n __typename\n totalCount\n edges {\n __typename\n node {\n __typename\n name\n }\n }\n pageInfo {\n __typename\n hasNextPage\n }\n isEmpty\n }\n ... on Droid {\n name\n primaryFunction\n }\n}",
6969
"fields": [
7070
{
7171
"responseName": "__typename",
@@ -179,6 +179,15 @@
179179
],
180180
"fragmentSpreads": [],
181181
"inlineFragments": []
182+
},
183+
{
184+
"responseName": "isEmpty",
185+
"fieldName": "isEmpty",
186+
"type": "Boolean!",
187+
"isConditional": false,
188+
"description": "For test java beans semantic naming only",
189+
"isDeprecated": false,
190+
"deprecationReason": null
182191
}
183192
],
184193
"fragmentSpreads": [],
@@ -305,6 +314,15 @@
305314
],
306315
"fragmentSpreads": [],
307316
"inlineFragments": []
317+
},
318+
{
319+
"responseName": "isEmpty",
320+
"fieldName": "isEmpty",
321+
"type": "Boolean!",
322+
"isConditional": false,
323+
"description": "For test java beans semantic naming only",
324+
"isDeprecated": false,
325+
"deprecationReason": null
308326
}
309327
],
310328
"fragmentSpreads": [],

apollo-compiler/src/test/graphql/com/example/java_beans_semantic_naming/TestQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public final class TestQuery implements Query<TestQuery.Data, Optional<TestQuery
3434
+ " }\n"
3535
+ "}";
3636

37-
public static final String OPERATION_ID = "c0463bc20566eabd263da887f6c45205c7b42522fa28585baf0e9f6021a2a8a6";
37+
public static final String OPERATION_ID = "4141e194c5f3846dabfcb576e735c71968b03a940baf49cc5e647c5e50eda72a";
3838

3939
public static final String QUERY_DOCUMENT = OPERATION_DEFINITION + "\n"
4040
+ HeroDetails.FRAGMENT_DEFINITION;

apollo-compiler/src/test/graphql/com/example/java_beans_semantic_naming/fragment/HeroDetails.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public interface HeroDetails extends GraphqlFragment {
3838
+ " __typename\n"
3939
+ " hasNextPage\n"
4040
+ " }\n"
41+
+ " isEmpty\n"
4142
+ " }\n"
4243
+ " ... on Droid {\n"
4344
+ " name\n"
@@ -97,6 +98,11 @@ interface FriendsConnection {
9798
*/
9899
@Nonnull PageInfo getPageInfo();
99100

101+
/**
102+
* For test java beans semantic naming only
103+
*/
104+
boolean isEmpty();
105+
100106
ResponseFieldMarshaller marshaller();
101107
}
102108

@@ -267,7 +273,8 @@ class FriendsConnection1 implements FriendsConnection {
267273
ResponseField.forString("__typename", "__typename", null, false, Collections.<ResponseField.Condition>emptyList()),
268274
ResponseField.forLong("totalCount", "totalCount", null, true, Collections.<ResponseField.Condition>emptyList()),
269275
ResponseField.forList("edges", "edges", null, true, Collections.<ResponseField.Condition>emptyList()),
270-
ResponseField.forObject("pageInfo", "pageInfo", null, false, Collections.<ResponseField.Condition>emptyList())
276+
ResponseField.forObject("pageInfo", "pageInfo", null, false, Collections.<ResponseField.Condition>emptyList()),
277+
ResponseField.forBoolean("isEmpty", "isEmpty", null, false, Collections.<ResponseField.Condition>emptyList())
271278
};
272279

273280
final @Nonnull String __typename;
@@ -278,18 +285,21 @@ class FriendsConnection1 implements FriendsConnection {
278285

279286
final @Nonnull PageInfo1 pageInfo;
280287

288+
final boolean isEmpty;
289+
281290
private volatile String $toString;
282291

283292
private volatile int $hashCode;
284293

285294
private volatile boolean $hashCodeMemoized;
286295

287296
public FriendsConnection1(@Nonnull String __typename, @Nullable Long totalCount,
288-
@Nullable List<Edge1> edges, @Nonnull PageInfo1 pageInfo) {
297+
@Nullable List<Edge1> edges, @Nonnull PageInfo1 pageInfo, boolean isEmpty) {
289298
this.__typename = Utils.checkNotNull(__typename, "__typename == null");
290299
this.totalCount = Optional.fromNullable(totalCount);
291300
this.edges = Optional.fromNullable(edges);
292301
this.pageInfo = Utils.checkNotNull(pageInfo, "pageInfo == null");
302+
this.isEmpty = isEmpty;
293303
}
294304

295305
public @Nonnull String get__typename() {
@@ -317,6 +327,13 @@ public Optional<List<Edge1>> getEdges() {
317327
return this.pageInfo;
318328
}
319329

330+
/**
331+
* For test java beans semantic naming only
332+
*/
333+
public boolean isEmpty() {
334+
return this.isEmpty;
335+
}
336+
320337
public ResponseFieldMarshaller marshaller() {
321338
return new ResponseFieldMarshaller() {
322339
@Override
@@ -330,6 +347,7 @@ public void write(Object value, ResponseWriter.ListItemWriter listItemWriter) {
330347
}
331348
});
332349
writer.writeObject($responseFields[3], pageInfo.marshaller());
350+
writer.writeBoolean($responseFields[4], isEmpty);
333351
}
334352
};
335353
}
@@ -341,7 +359,8 @@ public String toString() {
341359
+ "__typename=" + __typename + ", "
342360
+ "totalCount=" + totalCount + ", "
343361
+ "edges=" + edges + ", "
344-
+ "pageInfo=" + pageInfo
362+
+ "pageInfo=" + pageInfo + ", "
363+
+ "isEmpty=" + isEmpty
345364
+ "}";
346365
}
347366
return $toString;
@@ -357,7 +376,8 @@ public boolean equals(Object o) {
357376
return this.__typename.equals(that.__typename)
358377
&& this.totalCount.equals(that.totalCount)
359378
&& this.edges.equals(that.edges)
360-
&& this.pageInfo.equals(that.pageInfo);
379+
&& this.pageInfo.equals(that.pageInfo)
380+
&& this.isEmpty == that.isEmpty;
361381
}
362382
return false;
363383
}
@@ -374,6 +394,8 @@ public int hashCode() {
374394
h ^= edges.hashCode();
375395
h *= 1000003;
376396
h ^= pageInfo.hashCode();
397+
h *= 1000003;
398+
h ^= Boolean.valueOf(isEmpty).hashCode();
377399
$hashCode = h;
378400
$hashCodeMemoized = true;
379401
}
@@ -406,7 +428,8 @@ public PageInfo1 read(ResponseReader reader) {
406428
return pageInfo1FieldMapper.map(reader);
407429
}
408430
});
409-
return new FriendsConnection1(__typename, totalCount, edges, pageInfo);
431+
final boolean isEmpty = reader.readBoolean($responseFields[4]);
432+
return new FriendsConnection1(__typename, totalCount, edges, pageInfo, isEmpty);
410433
}
411434
}
412435
}

apollo-compiler/src/test/graphql/schema.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,22 @@
737737
},
738738
"isDeprecated": false,
739739
"deprecationReason": null
740+
},
741+
{
742+
"name": "isEmpty",
743+
"description": "For test java beans semantic naming only",
744+
"args": [],
745+
"type": {
746+
"kind": "NON_NULL",
747+
"name": null,
748+
"ofType": {
749+
"kind": "SCALAR",
750+
"name": "Boolean",
751+
"ofType": null
752+
}
753+
},
754+
"isDeprecated": false,
755+
"deprecationReason": null
740756
}
741757
],
742758
"inputFields": null,

0 commit comments

Comments
 (0)