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

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

Open
cfeltz34 opened this issue Mar 23, 2023 · 2 comments
Assignees
Labels
invalid This doesn't seem right
Projects

Comments

@cfeltz34
Copy link

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);
        }
    }
}
@matteobortolazzo
Copy link
Owner

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
Copy link
Owner

matteobortolazzo commented Jun 18, 2023

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

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

so it seems to work

@matteobortolazzo matteobortolazzo added the invalid This doesn't seem right label Jun 18, 2023
@matteobortolazzo matteobortolazzo self-assigned this Jun 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
CouchDB.NET
  
Awaiting triage
Development

No branches or pull requests

2 participants