fix: 🐛 Floats ending in .0 now being parsed as float type #874
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Float numbers ending in .0 where being parsed as integers in an OpenSearch instance when using the @searchable directive. If this data was the first being uploaded to the instance, searching queries wouldn't work as expected. The change creates a JSON file which is uploaded with the Python streaming function to lambda. The JSON has all the schema data types of the @searchable model fields. A modified version of the Python streaming function enables the DynamoDB event fields to be correctly parsed to the correct data type described in the schema.
✅ Closes: #866, #371
Description of changes
Changes
graphql-searchable-transformer.ts
This file had the following change:
Notice that
this.searchableObjectTypeDefinitions
was added as a parameter. This was needed as thesearchableObjectTypeDefinitions
has the schema that the user wants to create with its corresponding attributes and data types.create-streaming-lambda.ts
The
createLambda
function resides in this file and it had to be changed to create a JSON file with the following structure:This is later used to map the Schema data types to the corresponding Python data types, which solves the bug described in the issue
To create it, the
searchableObjectTypeDefinition
s was added as a parameter:Inside that function, just before the return statement, a helper function was created:
The helper function looks as follows:
Initially every model needs to be analyzed to find its corresponding name and attributes with their data types. Then for each of the fields in the model the data types must be extracted. To do that a helper function
findNamedType
needed to be created. The function looks like this:Recursion was needed for this function as the
TypeNode
interface permits nestedTypesNodes
inside of it according to its definition:Notice that the nesting ends with a
NamedTypeNode
as it doesn't allow moreListTypeNode
which allowTypeNode
themselves. Whenever theNamedTypeNode
is found, it extracts the attribute's data type and returns it.After every field and its data type has been extracted from its model it is added to the model's name and that happens for every model. After that a JSON file is generated and exported to the lib folder. Finally it is zipped with the
python_streaming_function.py
file to then be uploaded to the lambda function.package.json
The
package.json
needed to be changed as thepython_streaming_function.py
was being zipped duringyarn build
time.adm-zip
dependencies where also added.python_streaming_function.py
This file had to be changed to read the JSON file passed to the lambda and map to the corresponding GraphQL types found in the schema, which solves the issue of floats ending in .0 being parsed to int.
This new functions where added:
The
map_to_gql_types
function can be extended to solve other issues, such as when aAWSDate
is used with a timezone with the @serchable directive. For now it just fixes the Float being parsed to int issue. The function gets the serialized DynamoDB event fields and compares it with the fields data types in the JSON file. If it finds that there is a mismatch between data types, then it serializes it to the correct type found in the schema.Issue #, if available
This PR closes issues #866 and #371
Description of how you validated changes
Made a sample app without the changes implemented and used the same app with the changes implemented and tested both locally and in the cloud. Before the changes, whenever a register with a Float type ending with .0 was send, the index created in the OpenSearch instance would have a data type long. When making use of the searchable queries both through the app and directly through AppSync, the {eq: number} filter wouldn't work as expected. After the changes, the index created in the OpenSearch instance would have a data type float and the searchable queries would work as expected.
This was tested using different schemas with different data types and making use of relationships and other models with and without the @searchable directive.
All previous tests are running successfully, but no new tests where created.
Checklist
yarn test
passesBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.