Steve Coleman
Steve Coleman

Reputation: 2027

Servicestack AutoQuery not filtering results

my query /API/Json/GetJson?Desc=test1

I get all records not just the test1 records

[Route("/API/Json/GetJson", "GET")]
public class GetJson : QueryDb<JsonModel>
{
    public int? Id { get; set; }
    public int? RefId { get; set; }
    public int? SecondRefId { get; set; }
    public int? ThirdRefId { get; set; }
    public int? FourthRefId { get; set; }
    public string Name { get; set; }
    public string JSON { get; set; }
    public string JsonType { get; set; }
    public string Desc { get; set; }
    public int? AuditId { get; set; }
}

public class JsonModel
{
    [AutoIncrement]
    [PrimaryKey]
    [IgnoreOnUpdate]
    public int Id { get; set; }
    /// <summary>
    /// Other tables the this data is relevant to
    /// </summary>
    public int? RefId { get; set; }
    public int? SecondRefId { get; set; }
    public int? ThirdRefId { get; set; }
    public int? FourthRefId { get; set; }

    /// <summary>
    /// name that is displayed to users
    /// </summary>
    [Required]
    public string Name { get; set; }

    public string JSON { get; set; }
    /// <summary>
    /// Tells what data type the JSON is storing
    /// </summary>
    [Required]
    public string JsonType { get; set; }

    public string Desc { get; set; }

    public int AuditId { get; set; }

    public DateTime AuditStamp { get; set; } = DateTime.UtcNow;
}

also my return data has extra fields. Starting at Skip

{
    "id": 4,
    "refId": 9,
    "secondRefId": 3,
    "thirdRefId": 100,
    "fourthRefId": null,
    "name": "test",
    "json": "JSON STRING DATA",
    "jsonType": "test",
    "desc": "test3",
    "auditId": 0,
**"skip": null,
"take": null,
"orderBy": null,
"orderByDesc": null,
"include": null,
"fields": null,
"meta": null**
},

I updated my model to nullables and its is till returing all records. My seed data and I am using SS 5.6.0

WireUpService<IntegrationService>();

    using (var db = HostContext.Resolve<IDbConnectionFactory>().Open())
    {
        string JSON = " \"Columns\": [\r\n      {\r\n        \"CompanyName\": [\r\n          {\r\n            \"name\": \"Company Name\",\r\n            \"Visible\": \"True\",\r\n            \"Sort\": \"U,A,Z[Unsorted, A-Z, Z-A]\",\r\n            \"Filter\": \"Test Company\"\r\n          }\r\n        ],\r\n        \"ParentCompnay\": [\r\n          {\r\n            \"name\": \"Company Name\",\r\n            \"Visible\": \"True\",\r\n            \"Sort\": \"U,A,Z[Unsorted, A-Z, Z-A]\",\r\n            \"Filter\": \"Test Company\"\r\n          }\r\n        ]\r\n      }";
        db.DropAndCreateTable<JsonModel>();
        db.Insert(new JsonModel { Desc = "test",Name = "test",JsonType = "test", JSON = JSON,RefId = 10,SecondRefId = 3, AuditId = 0, AuditStamp = DateTime.Now });
        db.Insert(new JsonModel { Desc = "test1", Name = "test", JsonType = "test", JSON = JSON, RefId = 10, SecondRefId = 3, AuditId = 0, AuditStamp = DateTime.Now });
        db.Insert(new JsonModel { Desc = "test2", Name = "test", JsonType = "test", JSON = JSON, RefId = 5, SecondRefId = 3, AuditId = 0, AuditStamp = DateTime.Now });
        db.Insert(new JsonModel { Desc = "test3", Name = "test", JsonType = "test", JSON = JSON, RefId = 9, SecondRefId = 3,ThirdRefId = 100, AuditId = 0, AuditStamp = DateTime.Now });
    }

Upvotes: 0

Views: 69

Answers (1)

mythz
mythz

Reputation: 143319

I wasn't able to reproduce this issue using the classes provided, which I've seeded with test data that matches the query and non-matching test data that you've included in your JSON response:

db.CreateTable<JsonModel>();
db.Insert(new JsonModel { RefId = 1, SecondRefId = 1, ThirdRefId = 111, Name = "test1", Desc = "test1", JsonType = "test", JSON = "TEST1"});
db.Insert(new JsonModel { RefId = 9, SecondRefId = 3, ThirdRefId = 100, Name = "test1", Desc = "test3", JsonType = "test", JSON = "JSON STRING DATA"});

There is an issue with your GetJson model where you've specified Id and AuditId as non-nullable int properties which if not specified are populated in your GetJson Request DTO as 0 (default int).

If you're going to include required value types on your GetJson AutoQuery Service you should always be providing values for them otherwise change them into int? so they're not added to the query filter when they're not specified, e.g:

public class GetJson : QueryDb<JsonModel>
{
    public int? Id { get; set; }
    public int? AuditId { get; set; }
}

After doing this, it works as expected where I can query the Auto Query Service using your specified query, i.e:

var url = baseUrl.CombineWith("/API/Json/GetJson").AddQueryParam("Desc", "test1");
var json = url.GetJsonFromUrl();
json.Print();

Which works as expected returning the matching result in a QueryResponse<T> DTO, i.e:

{"Offset":0,"Total":0,"Results":[{"Id":1,"RefId":1,"SecondRefId":1,"ThirdRefId":111,"Name":"test1","JSON":"TEST1","JsonType":"test","Desc":"test1","AuditId":0,"AuditStamp":"\/Date(1577190306230-0000)\/"}],"Meta":{}}

Upvotes: 1

Related Questions