Skip to content
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
54 changes: 32 additions & 22 deletions src/main/java/com/kobylynskyi/graphql/codegen/GraphQLCodegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -90,38 +93,45 @@ protected GraphQLCodegen(List<String> schemas,

initDefaultValues(mappingConfig);
validateConfigs(mappingConfig);
sanitizeValues(mappingConfig);
sanitize(mappingConfig);
}

private static void sanitizeValues(MappingConfig mappingConfig) {
private static void sanitize(MappingConfig mappingConfig) {
mappingConfig.setModelValidationAnnotation(
Utils.replaceLeadingAtSign(mappingConfig.getModelValidationAnnotation()));

if (mappingConfig.getResolverArgumentAnnotations() != null) {
mappingConfig.setResolverArgumentAnnotations(mappingConfig.getResolverArgumentAnnotations().stream()
.map(Utils::replaceLeadingAtSign).collect(Collectors.toSet()));
mappingConfig.setResolverArgumentAnnotations(sanitizeSet(mappingConfig.getResolverArgumentAnnotations()));
mappingConfig.setParametrizedResolverAnnotations(
sanitizeSet(mappingConfig.getParametrizedResolverAnnotations()));
mappingConfig.setCustomAnnotationsMapping(sanitizeMap(mappingConfig.getCustomAnnotationsMapping()));
mappingConfig.setDirectiveAnnotationsMapping(sanitizeMap(mappingConfig.getDirectiveAnnotationsMapping()));
if (mappingConfig.getCustomTypesMapping() != null) {
mappingConfig.setCustomTypesMapping(new HashMap<>(mappingConfig.getCustomTypesMapping()));
}
if (mappingConfig.getParametrizedResolverAnnotations() != null) {
mappingConfig.setParametrizedResolverAnnotations(mappingConfig.getParametrizedResolverAnnotations().stream()
.map(Utils::replaceLeadingAtSign).collect(Collectors.toSet()));
if (mappingConfig.getCustomTemplates() != null) {
mappingConfig.setCustomTemplates(new HashMap<>(mappingConfig.getCustomTemplates()));
}
}

Map<String, List<String>> customAnnotationsMapping = mappingConfig.getCustomAnnotationsMapping();
if (customAnnotationsMapping != null) {
for (Map.Entry<String, List<String>> entry : customAnnotationsMapping.entrySet()) {
if (entry.getValue() != null) {
entry.setValue(entry.getValue().stream().map(Utils::replaceLeadingAtSign).collect(toList()));
}
}
private static Set<String> sanitizeSet(Set<String> originalSet) {
if (originalSet == null) {
return new HashSet<>();
}
return originalSet.stream().map(Utils::replaceLeadingAtSign).collect(Collectors.toSet());
}

private static Map<String, List<String>> sanitizeMap(Map<String, List<String>> multiValueMap) {
if (multiValueMap == null) {
return new HashMap<>();
}
Map<String, List<String>> directiveAnnotationsMapping = mappingConfig.getDirectiveAnnotationsMapping();
if (directiveAnnotationsMapping != null) {
for (Map.Entry<String, List<String>> entry : directiveAnnotationsMapping.entrySet()) {
if (entry.getValue() != null) {
entry.setValue(entry.getValue().stream().map(Utils::replaceLeadingAtSign).collect(toList()));
}
Map<String, List<String>> sanitizedMultiValueMap = new HashMap<>(multiValueMap.size());
for (Map.Entry<String, List<String>> entry : multiValueMap.entrySet()) {
List<String> sanitizedValues = null;
if (entry.getValue() != null) {
sanitizedValues = entry.getValue().stream().map(Utils::replaceLeadingAtSign).collect(toList());
}
sanitizedMultiValueMap.put(entry.getKey(), sanitizedValues);
}
return sanitizedMultiValueMap;
}

protected void initDefaultValues(MappingConfig mappingConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,10 @@ void cleanup() {

@Test
void generate_CustomAnnotationMappings() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(
singletonMap("Event.createdDateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Event.createdDateTime",
mappingConfig.setCustomTypesMapping(singletonMap("Event.createdDateTime", "org.joda.time.DateTime"));
mappingConfig.setCustomAnnotationsMapping(singletonMap("Event.createdDateTime",
singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));
"using = com.example.json.DateTimeScalarDeserializer.class)")));

generate("src/test/resources/schemas/test.graphqls");

Expand All @@ -63,10 +62,10 @@ void generate_CustomAnnotationMappings() throws Exception {

@Test
void generate_CustomAnnotationMappings_Type() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("DateTime",
mappingConfig.setCustomTypesMapping(singletonMap("DateTime", "org.joda.time.DateTime"));
mappingConfig.setCustomAnnotationsMapping(singletonMap("DateTime",
singletonList("com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));
"using = com.example.json.DateTimeScalarDeserializer.class)")));

generate("src/test/resources/schemas/test.graphqls");

Expand All @@ -92,10 +91,10 @@ void generate_CustomAnnotationMappings_Input() throws Exception {

@Test
void generate_CustomAnnotationMappings_Regexp() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Date.*",
mappingConfig.setCustomTypesMapping(singletonMap("DateTime", "org.joda.time.DateTime"));
mappingConfig.setCustomAnnotationsMapping(singletonMap("Date.*",
singletonList("com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));
"using = com.example.json.DateTimeScalarDeserializer.class)")));

generate("src/test/resources/schemas/test.graphqls");

Expand All @@ -108,10 +107,10 @@ void generate_CustomAnnotationMappings_Regexp() throws Exception {

@Test
void generate_CustomAnnotationMappings_FieldType() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Event.createdDateTime",
mappingConfig.setCustomTypesMapping(singletonMap("DateTime", "org.joda.time.DateTime"));
mappingConfig.setCustomAnnotationsMapping(singletonMap("Event.createdDateTime",
singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));
"using = com.example.json.DateTimeScalarDeserializer.class)")));

generate("src/test/resources/schemas/test.graphqls");

Expand All @@ -124,10 +123,10 @@ void generate_CustomAnnotationMappings_FieldType() throws Exception {

@Test
void generate_CustomAnnotationMappings_FieldType_Regexp() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Event..*Date.*",
mappingConfig.setCustomTypesMapping(singletonMap("DateTime", "org.joda.time.DateTime"));
mappingConfig.setCustomAnnotationsMapping(singletonMap("Event..*Date.*",
singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));
"using = com.example.json.DateTimeScalarDeserializer.class)")));

generate("src/test/resources/schemas/test.graphqls");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void cleanup() {

@Test
void generate_CustomTypeMapping_WholeScalar() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("ZonedDateTime", "String")));
mappingConfig.setCustomTypesMapping(singletonMap("ZonedDateTime", "String"));

generate("src/test/resources/schemas/date-scalar.graphqls");

Expand Down Expand Up @@ -74,7 +74,7 @@ void generate_CustomTypeMapping_ScalarOnQueryOnly() throws Exception {
@Test
void generate_CustomTypeMapping_ForExtensionProperty() throws Exception {
mappingConfig.setGenerateExtensionFieldsResolvers(true);
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("External", "com.example.External")));
mappingConfig.setCustomTypesMapping(singletonMap("External", "com.example.External"));
mappingConfig.setGenerateClient(false);

generate("src/test/resources/schemas/external-type-extend.graphqls");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -87,7 +86,7 @@ void generate_CheckFiles_WithPrefixSuffix() throws Exception {

@Test
void generate_CheckFiles_OnLongDefault() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("Long", "java.lang.Long")));
mappingConfig.setCustomTypesMapping(singletonMap("Long", "java.lang.Long"));
mappingConfig.setModelNameSuffix("DTO");

generate("src/test/resources/schemas/defaults-with-Long.graphqls");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.HashMap;
import java.util.Objects;

import static com.kobylynskyi.graphql.codegen.TestUtils.assertFileContainsElements;
import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
import static com.kobylynskyi.graphql.codegen.TestUtils.getFileByName;
import static java.util.Collections.singletonMap;

@ExtendWith(MaxQueryTokensExtension.class)
class GraphQLCodegenOptionalTest {
Expand Down Expand Up @@ -106,7 +105,7 @@ void generate_OptionalFieldInInterfaceAndMandatoryInType() throws Exception {
*/
@Test
void generate_ObjectsInsteadOfPrimitives() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(Collections.singletonMap("Int!", "Integer")));
mappingConfig.setCustomTypesMapping(singletonMap("Int!", "Integer"));
schemaFinder.setIncludePattern("optional-vs-mandatory-types.graphqls");

generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.io.IOException;
import java.nio.file.NoSuchFileException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -93,7 +92,7 @@ void generate_NoBuilder() throws Exception {

@Test
void generate_CustomMappings() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "java.util.Date")));
mappingConfig.setCustomTypesMapping(singletonMap("DateTime", "java.util.Date"));

generate("src/test/resources/schemas/test.graphqls");

Expand All @@ -103,8 +102,7 @@ void generate_CustomMappings() throws Exception {

@Test
void generate_CustomMappings_Nested() throws Exception {
mappingConfig
.setCustomTypesMapping(new HashMap<>(singletonMap("EventProperty.intVal", "java.math.BigInteger")));
mappingConfig.setCustomTypesMapping(singletonMap("EventProperty.intVal", "java.math.BigInteger"));

generate("src/test/resources/schemas/test.graphqls");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Objects;

import static com.kobylynskyi.graphql.codegen.TestUtils.assertFileContainsElements;
Expand Down Expand Up @@ -43,11 +42,10 @@ void cleanup() {

@Test
void generate_CustomAnnotationMappings() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(
singletonMap("Event.createdDateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Event.createdDateTime",
mappingConfig.setCustomTypesMapping(singletonMap("Event.createdDateTime", "org.joda.time.DateTime"));
mappingConfig.setCustomAnnotationsMapping(singletonMap("Event.createdDateTime",
singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));
"using = com.example.json.DateTimeScalarDeserializer.class)")));

generate("src/test/resources/schemas/test.graphqls");

Expand All @@ -60,10 +58,10 @@ void generate_CustomAnnotationMappings() throws Exception {

@Test
void generate_CustomAnnotationMappings_Type() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("DateTime",
mappingConfig.setCustomTypesMapping(singletonMap("DateTime", "org.joda.time.DateTime"));
mappingConfig.setCustomAnnotationsMapping(singletonMap("DateTime",
singletonList("com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));
"using = com.example.json.DateTimeScalarDeserializer.class)")));

generate("src/test/resources/schemas/test.graphqls");

Expand All @@ -76,8 +74,8 @@ void generate_CustomAnnotationMappings_Type() throws Exception {

@Test
void generate_CustomAnnotationMappings_Input() throws Exception {
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("ReproInput.reproField",
singletonList("@com.fasterxml.jackson.annotation.JsonProperty(\"reproField\")"))));
mappingConfig.setCustomAnnotationsMapping(singletonMap("ReproInput.reproField",
singletonList("@com.fasterxml.jackson.annotation.JsonProperty(\"reproField\")")));

generate("src/test/resources/schemas/input.graphqls");

Expand All @@ -89,10 +87,10 @@ void generate_CustomAnnotationMappings_Input() throws Exception {

@Test
void generate_CustomAnnotationMappings_Regexp() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Date.*",
mappingConfig.setCustomTypesMapping(singletonMap("DateTime", "org.joda.time.DateTime"));
mappingConfig.setCustomAnnotationsMapping(singletonMap("Date.*",
singletonList("com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));
"using = com.example.json.DateTimeScalarDeserializer.class)")));

generate("src/test/resources/schemas/test.graphqls");

Expand All @@ -105,10 +103,10 @@ void generate_CustomAnnotationMappings_Regexp() throws Exception {

@Test
void generate_CustomAnnotationMappings_FieldType() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("DateTime", "org.joda.time.DateTime")));
mappingConfig.setCustomAnnotationsMapping(new HashMap<>(singletonMap("Event.createdDateTime",
mappingConfig.setCustomTypesMapping(singletonMap("DateTime", "org.joda.time.DateTime"));
mappingConfig.setCustomAnnotationsMapping(singletonMap("Event.createdDateTime",
singletonList("@com.fasterxml.jackson.databind.annotation.JsonDeserialize(" +
"using = com.example.json.DateTimeScalarDeserializer.class)"))));
"using = com.example.json.DateTimeScalarDeserializer.class)")));

generate("src/test/resources/schemas/test.graphqls");

Expand All @@ -121,10 +119,9 @@ void generate_CustomAnnotationMappings_FieldType() throws Exception {

@Test
void generate_CustomAnnotationMappings_With_Annotations() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(
singletonMap("CAMS", "com.intuit.identity.manage.enum.CamsGroup::class")));
mappingConfig.setDirectiveAnnotationsMapping(new HashMap<>(singletonMap("NotNull",
singletonList("@javax.validation.constraints.NotNull(message = {{message}}, groups = {{groups}})"))));
mappingConfig.setCustomTypesMapping(singletonMap("CAMS", "com.intuit.identity.manage.enum.CamsGroup::class"));
mappingConfig.setDirectiveAnnotationsMapping(singletonMap("NotNull",
singletonList("@javax.validation.constraints.NotNull(message = {{message}}, groups = {{groups}})")));

generate("src/test/resources/schemas/kt/customTypesMapping-directive.graphqls");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void cleanup() {

@Test
void generate_CustomTypeMapping_WholeScalar() throws Exception {
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("ZonedDateTime", "String")));
mappingConfig.setCustomTypesMapping(singletonMap("ZonedDateTime", "String"));

generate("src/test/resources/schemas/date-scalar.graphqls");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Objects;

import static com.kobylynskyi.graphql.codegen.TestUtils.assertSameTrimmedContent;
Expand Down Expand Up @@ -117,7 +116,7 @@ void generate_OptionalFieldInInterfaceAndMandatoryInType() throws Exception {
void generate_NullableCustomTypeWithApiReturnType() throws Exception {
mappingConfig.setGenerateApis(true);
mappingConfig.setApiReturnType("reactor.core.publisher.Mono");
mappingConfig.setCustomTypesMapping(new HashMap<>(singletonMap("ZonedDateTime", "String")));
mappingConfig.setCustomTypesMapping(singletonMap("ZonedDateTime", "String"));

schemaFinder.setIncludePattern("nullable-custom-type-with-api-return-type.graphqls");

Expand Down
Loading