From a42a0580a05929e4a991b6c2c376dac0b24e9785 Mon Sep 17 00:00:00 2001 From: Andrea Di Cesare Date: Fri, 20 Oct 2023 18:08:22 +0200 Subject: [PATCH] :bug: Fix missing rootDoc for GraphQL queries involving only non-mapped fields at path level 2 --- .../graphql/models/builder/AppBuilder.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/graphql/src/main/java/org/restheart/graphql/models/builder/AppBuilder.java b/graphql/src/main/java/org/restheart/graphql/models/builder/AppBuilder.java index 96454cb701..6de6a38a18 100644 --- a/graphql/src/main/java/org/restheart/graphql/models/builder/AppBuilder.java +++ b/graphql/src/main/java/org/restheart/graphql/models/builder/AppBuilder.java @@ -20,6 +20,7 @@ */ package org.restheart.graphql.models.builder; +import java.util.HashMap; import java.util.Map; import org.bson.BsonDocument; @@ -27,6 +28,7 @@ import org.restheart.graphql.GraphQLIllegalAppDefinitionException; import org.restheart.graphql.models.AppDescriptor; import org.restheart.graphql.models.GraphQLApp; +import org.restheart.graphql.models.ObjectMapping; import org.restheart.graphql.models.TypeMapping; import org.restheart.utils.BsonUtils; @@ -71,6 +73,8 @@ public static final GraphQLApp build(BsonDocument appDef) throws GraphQLIllegalA throw new GraphQLIllegalAppDefinitionException(errorMSg, schemaProblem); } + + if (appDef.containsKey("mappings")) { if (appDef.get("mappings").isDocument()) { var mappings = appDef.getDocument("mappings"); @@ -82,15 +86,19 @@ public static final GraphQLApp build(BsonDocument appDef) throws GraphQLIllegalA throw new GraphQLIllegalAppDefinitionException("'Mappings' field must be an Object but was " + appDef.get("mappings").getBsonType()); } } else { - objectsMappings = null; + objectsMappings = new HashMap<>(); } // Provide a default field mappings for Objects that are not explicitly mapped. // see ObjectsMappings.defaultObjectFieldMappings() javadoc for more information. typeDefinitionRegistry.types().entrySet().stream() - .filter(e -> objectsMappings == null || !objectsMappings.containsKey(e.getKey())) + .filter(e -> !objectsMappings.containsKey(e.getKey())) .filter(e -> e.getValue() instanceof ObjectTypeDefinition) - .forEach(e -> ObjectsMappings.defaultObjectFieldMappings(e.getKey(), typeDefinitionRegistry, new BsonDocument())); + .forEach(e -> { + var objectFieldMappings = ObjectsMappings.defaultObjectFieldMappings(e.getKey(), typeDefinitionRegistry, new BsonDocument()); + var objectMapping = new ObjectMapping(e.getKey(), objectFieldMappings); + objectsMappings.put(e.getKey(), objectMapping); + }); try { return GraphQLApp.newBuilder().appDescriptor(descriptor).schema(schema)