couchdb-net icon indicating copy to clipboard operation
couchdb-net copied to clipboard

Database Splitting + Convert --> Crash : Exception thrown: Call failed with status code 400 (Bad Request)

Open cfeltz34 opened this issue 1 year ago • 2 comments

I have an object in CouchDB with a lot en fields. I just want to get light objects.

Is ok if I execute this command :

var couchDocuments = couchDatabase.Take(TAKE_LIMIT).Select(d => d.Id, d => d.Rev).ToList();
--> couchdb request parameters is : 
"{\"limit\":1000,\"fields\":[\"_id\",\"_rev\"],\"selector\": {\"split_discriminator\":\"ExampleDataCouchDB\"}}"

But my object is not correctly typed

If I execute this command :

var couchDocuments2 = couchDatabase.Take(TAKE_LIMIT).Convert<TCouchDocument, TypedCouchDocumentBase>().ToList();
--> couchdb request parameters is : 
"{\"selector\":{\"split_discriminator\":\"ExampleDataCouchDB\"}}\"typeMessage\",\"errorMessage\"],\"selector\":{\"split_discriminator\":\"ExampleDataCouchDB\"}}"

A comma is missing --> Crash : Exception thrown: 'Flurl.Http.FlurlHttpException' in System.Private.CoreLib.dll: 'Call failed with status code 400 (Bad Request): POST http://127.0.0.1:5984/example/_find'

If I execute this command :

var couchDocuments3 = couchDatabase.Take(TAKE_LIMIT).Convert<TCouchDocument, CouchDocumentBase>().ToList();
--> couchdb request parameters is : 
"{\"selector\":{\"split_discriminator\":\"ExampleDataCouchDB\"}],\"selector\":{\"split_discriminator\":\"ExampleDataCouchDB\"}}"

No fields have been selected --> Crash : Exception thrown: 'Flurl.Http.FlurlHttpException' in System.Private.CoreLib.dll: 'Call failed with status code 400 (Bad Request): POST http://127.0.0.1:5984/example/_find'

Remark : In VisitConvertMethod method, why you have this condition : ".Where(p => p.DeclaringType != typeof(CouchDocument))" ? How can I get an object containing only the Id and the revision (Rev) ?

Remark 2 : For both "Convert" case, Take(1000) is not added on request

    public class TypedCouchDocumentBase : CouchDocumentBase, IDto, EntityWithKeyAsString
    {
        virtual public string typeMessage { get; set; }
        virtual public string errorMessage { get; set; }

        public TypedCouchDocumentBase() { 
        }
    }

    public class CouchDocumentBase : CouchDocument
    {
        public CouchDocumentBase() { 
        }
    }
using System;
using System.Threading.Tasks;
using CouchDB.Driver;
using CouchDB.Driver.Options;
using ExampleApi.Models;
using ExampleApi.ModelsCouchDB;
using Microsoft.Extensions.Logging;

namespace ExampleApi.CouchDB
{
    public class CouchDBContext : CouchContext
    {
        public string DatabaseName = "example";

        public CouchDatabase<ExampleDataCouchDB> ExampleDatas { get; set; }
        public CouchDatabase<DesignDocument> ExampleDesign { get; set; }

        protected override void OnDatabaseCreating(CouchDatabaseBuilder databaseBuilder)
        {
            databaseBuilder.Document<ExampleDataCouchDB>().ToDatabase(this.DatabaseName);
            databaseBuilder.Document<DesignDocument>().ToDatabase(this.DatabaseName);
        }
    }
}

cfeltz34 avatar Mar 23 '23 15:03 cfeltz34

Hi, about the Where(p => p.DeclaringType != typeof(CouchDocument)), it's probably because I didn't want to request stuff like _deleted, _attachments, _conflicts and split_discriminator. If you use that simpler DTO to read-only you can add [JsonProperty("_id"] and not inherit from CouchDocument.

matteobortolazzo avatar Jun 18 '23 15:06 matteobortolazzo

As per remark 2 I added a unit test and I get

{
  "limit": 1,
  "fields": [
    "name",
    "age"
  ],
  "selector": {}
}

so it seems to work

matteobortolazzo avatar Jun 18 '23 15:06 matteobortolazzo