Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add null checks for JSON stream #154

Open
Seelenoede opened this issue Oct 9, 2024 · 2 comments
Open

Add null checks for JSON stream #154

Seelenoede opened this issue Oct 9, 2024 · 2 comments
Assignees

Comments

@Seelenoede
Copy link

In a project, I tried to create a mock Order record with a few OrderItems. But then makeRelationship failed.
After further digging, I noticed that one of the Product2 had a field with a value, while another Product2 had this field empty, which then lead to an issue with the "streamTokens method in fflib_ApexMocksUtils, as it couldn't parse the null token.
So basically it tried to write the JSON as
{ "data": "xyz",
"data2":,
"data3: "abc"}
which is of course is an invalid JSON.

I could fix it by adding

else if(currentToken == JSONToken.VALUE_NULL)
	toStream.writeNull();

to the method.

I also saw that there is no parsing for NOT_AVAILABLE and VALUE_EMBEDDED_OBJECT, although I don't think, that is an issue.

It would be great to have the fix from above part of the code, unless there is a good reason why we shouldn't parse null values.

@ImJohnMDaniel
Copy link
Contributor

@Seelenoede -- Thanks for letting us know about this issue. Any chance that you could create some examples demonstrating the behavior, please? It would help us resolve this quicker. I appreciate it.

@ImJohnMDaniel ImJohnMDaniel self-assigned this Nov 29, 2024
@Seelenoede
Copy link
Author

Seelenoede commented Dec 2, 2024

@ImJohnMDaniel sure. I could reproduce this on a Trailhead Playground with the current version of apex-mocks. A set up an unit test.
What I am doing is, I create at least two Product2 records. Both have the same fields filled, but one field is set to null.
Then I create an OrderItem record for each of the products and finally add my order items to an Order with fflib_ApexMocksUtils.makeRelationship().
If I run the test, makeRelationship will throw the JSONException as it will not parse the null value of the Product2 correctly.

@IsTest
public with sharing class fflibUnitTest {
    @IsTest
    static void shouldNotThrowErrorWhenMockingRelationship() {
        try {
            Product2 prod1 = new Product2(
                Id = fflib_IDGenerator.generate(Product2.SObjectType),
                Name = 'Product1',
                ProductCode = 'P1',
                Description = null,
                StockKeepingUnit = 'P1'
            );

            Product2 prod2 = new Product2(
                Id = fflib_IDGenerator.generate(Product2.SObjectType),
                Name = 'Product2',
                ProductCode = 'P2',
                Description = 'this is another product',
                StockKeepingUnit = 'P2'
            );

            OrderItem oi1 = new OrderItem(
                Id = fflib_IDGenerator.generate(OrderItem.SObjectType),
                Product2Id = prod1.Id,
                Product2 = prod1,
                UnitPrice = 10,
                Quantity = 1
            );

            OrderItem oi2 = new OrderItem(
                Id = fflib_IDGenerator.generate(OrderItem.SObjectType),
                Product2Id = prod2.Id,
                Product2 = prod2,
                UnitPrice = 10,
                Quantity = 1
            );

            Order order = new Order();

            order = ((List<Order>) fflib_ApexMocksUtils.makeRelationship(
                List<Order>.class,
                new List<Order>{
                    order
                },
                OrderItem.OrderId,
                new List<List<OrderItem>>{
                    new List<OrderItem>{oi1, oi2}
                }
            ))[0];
        } catch (JSONException e) {
            Assert.fail('Should not throw a JSONException');
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants