Home > OS >  Fiebase rules creation using c# returning System.NullReferenceException, how to fix this issue?
Fiebase rules creation using c# returning System.NullReferenceException, how to fix this issue?

Time:11-16

Created the auth function for firebase rule service and called the service instance to create the firebase ruleset but it is returning System.NullReferenceException: Object reference not set to an instance of an object.What did I miss here ?

using Google.Apis.FirebaseRules.v1;
using Ruleset = Google.Apis.FirebaseRules.v1.Data.Ruleset;
using Source = Google.Apis.FirebaseRules.v1.Data.Source;
using File = Google.Apis.FirebaseRules.v1.Data.File;

private static FirebaseRulesService _firebaseRulesService;


public static void IntializeFirebaseRules() {
    GoogleCredential credential = GoogleCredential.GetApplicationDefault();
    if (CloudManager.Credential.IsCreateScopedRequired)
    {
        credential = CloudManager.Credential.CreateScoped(FirebaseRulesService.Scope.CloudPlatform);
    }
    _firebaseRulesService = new FirebaseRulesService(
    new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = CloudManager.ApplicationName

        });
}
    

 public static async  void AddRuleset()
{
    Ruleset myRuleset = new Ruleset {
    new Source {
        Files = {
        new File {
        Name = "firestore.rules",
        Content = "service cloud.firestore {"  
        "match /databases/{database}/documents {"  
        "match /users/{userId}{"  
        "allow read: if (request.auth.uid == userId);"  
        "}"  
        "match /licenses/{accountId} {"  
        "allow read: if isAccountOwner(accountId);"  
        "}"  
        "}" 
        "}",
            }
            }
        };
    };


    _firebaseRulesService.Projects.Rulesets.Create(myRuleset , "projects/"   CloudManager.ProjectId);
}

**Edited -> separated the objects as below and the NullReferenceException occurrs on the line Source mySource = new Source

File myFile =  new File {
    Name = "firestore.rules",
    Content = "service cloud.firestore {"  
    "match /databases/{database}/documents {"  
    "match /users/{userId}{"  
    "allow read: if (request.auth.uid == userId);"  
    "}"  
    "function doc(subpath) {"  
    "return get(/databases/$(database)/documents/$(subpath)).data;"  
    "}"  
    "function isAccountOwner(accountId) {"  
    "return request.auth.uid == accountId"   
    "|| doc(/users/$(request.auth.uid)).accountId == accountId;"  
    "}"  
    "match /licenses/{accountId} {"  
    "allow read: if isAccountOwner(accountId);"  
    "}"  
    "}" 
    "}",
};
Source mySource = new Source {
    Files = {
        myFile
    }  
};
Ruleset myRuleset = new Ruleset {
    Source = mySource
};

_firebaseRulesService.Projects.Rulesets.Create(myRuleset , "projects/"   CloudManager.ProjectId);

CodePudding user response:

This statement:

Source mySource = new Source {
    Files = {
        myFile
    }  
};

... is trying to add myFile to the Files collection - but I suspect you'll find that's null by default. You can initialize it to a list instead:

Source mySource = new Source { Files = new List<File> { myFile } };
  • Related