ballerina-platform / ballerina-lang

The Ballerina Programming Language
https://ballerina.io/
Apache License 2.0
3.54k stars 733 forks source link

[Bug]: Invalid coverage report generated for GraphQL service #42640

Closed MaryamZi closed 2 weeks ago

MaryamZi commented 2 weeks ago

Description

$title.

Steps to Reproduce

Source

import ballerina/graphql;

type Person record {|
    readonly int id;
    string firstName;
    string lastName;
    int age;
|};

table<Person> key(id) data = table [
    {id: 101, firstName: "John", lastName: "Doe", age: 20},
    {id: 102, firstName: "Mary", lastName: "Anne", age: 30}
];

@graphql:ServiceConfig {
    graphiql: {
        enabled: true
    }
}
service /graphql on new graphql:Listener(9090) {
    resource function get profiles() returns Profile[] {
        return from int id in data.keys() select new (id);
    }
    resource function get profile(@graphql:ID int id) returns Profile {
        return new (id);
    }
}

service class Profile {
    private final int id;
    private final string firstName;
    private final string lastName;
    private final int age;

    function init(int id) {
        Person {firstName, lastName, age} = data.get(id);
        self.id = id;
        self.firstName = firstName;
        self.lastName = lastName;
        self.age = age;
    }

    resource function get id() returns @graphql:ID int {
        return self.id;
    }

    resource function get fullName() returns string {
        return self.firstName + " " + self.lastName;
    }

    resource function get firstName() returns string {
        return self.firstName;
    }

    resource function get lastName() returns string {
        return self.lastName;
    }

    resource function get age() returns int {
        return self.age;
    }

    resource function get isAdult() returns boolean {
        return self.age > 21;
    }
}

Tests

import ballerina/graphql;
import ballerina/test;

final graphql:Client cl = check new ("http://localhost:9090/graphql");

@test:Config
function testRetrievingProfile() returns error? {
    json payload = check cl->execute(string `query QueryProfile($id: ID!) {
        profile(id: $id) {
            fullName
            age
        }
    }`, {id: 101});
    test:assertEquals(payload, {
        "data": {
            "profile": {
                "fullName": "John Doe",
                "age": 20
            }
        }
    });
}

Screenshot from 2024-04-25 17-42-48

Affected Version(s)

2201.9.0-rc1

OS, DB, other environment details and versions

No response

Related area

-> Test Framework

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

gayaldassanayake commented 2 weeks ago

The code modifier within the graphql package adds a new field to the annotation @graphql:ServiceConfig. However, during this process, the line breaks of the annotation were lost.

- @graphql:ServiceConfig {
-   graphiql: {
-       enabled: true
-   }
- }
- service /reviewed on new graphql:Listener(9000) {
+ @graphql:ServiceConfig {         graphiql: {
+       enabled: true
+   },
+, schemaString: "rO0A...." ,fieldCacheConfig:{enabled:false,maxSize:0}}service /reviewed on new graphql:Listener(9000) {

From the code coverage POV, this causes a multi-line diff of the CHANGE type. This results in offsetting up coverage of the lines following the annotation.

As per the offline discussions with @azinneera @MaryamZi @SasinduDilshara @lochana-chathura , this needs to be fixed in the GraphQL compiler plugin. More details on this are documented in the library issue to track this https://github.com/ballerina-platform/ballerina-library/issues/6418.

Closing this issue for now.

github-actions[bot] commented 2 weeks ago

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.