30

I'm getting some weird errors from protorpc when I use endpoints. In this code:

class Application(EndpointsModel):

    _message_fields_schema = ('id', 'name')

    created = ndb.DateTimeProperty(auto_now_add=True)
    name = ndb.StringProperty()
    roles = ndb.IntegerProperty(repeated=True)
    updated = ndb.DateTimeProperty(auto_now=True)
    owner = ndb.KeyProperty(kind='User')

@API.api_class(resource_name="application")
class ApplicationApi(protorpc.remote.Service):

    @Application.method(http_method="GET",
                        request_fields=('id',),
                        name="get",
                        path="applications/{id}")
    def ApplicationGet(self, instance):
        if not instance.from_datastore:
            raise endpoints.NotFoundException("Application not found.")
        return instance

    @Application.query_method(http_method="GET",
                              query_fields=('limit', 'order', 'pageToken'),
                              name="list",
                              path="applications")
    def ApplicationList(self, query):
        return query

when I call application.get() error is as follows: (full trace here):

TypeError: Can only copy from entities of the exact type Application. Received an instance of Application.

and for calling application.list() error is as follows: (full trace here):

ValidationError: Expected type <class '.Application'> for field items, found <Application name: u'test'> (type <class '.Application'>)

What could be causing this? My other models with pretty much the same code (just different properties) work fine.

Sanchit
  • 315
  • 2
  • 20
Sasxa
  • 40,334
  • 16
  • 88
  • 102
  • Those traces look like you are using an instance of `Application` instead of the class(type) object `Application`. Are you sure you aren't using `Application()` somewhere in your code? – Josh J Feb 23 '16 at 21:46
  • Just double-checked, I'm not using it anywhere - all relevant code is in the question. I am extending a class `ApplicationApi(BaseRemoteService)`, but no mention of Application there, and there's a POST api method (which works...), but even if I remove all that I'm still getting the same errors with just the code from OP. – Sasxa Feb 23 '16 at 22:50
  • 1
    It's a shot in the dark, but prior to subclassing `EndpointsModel`, did `Application` subclass `ndb.Model`? Try looping through all `Application` entities and doing a `put` again on them. I'm thinking that the key you pass in fetches the entity but something about the inheritance chain is making the `__class__` fields not match. – Josh J Feb 24 '16 at 15:37
  • 3
    You might be right. The weird thing is `Application` class didn't subclass anything other then `EndpointsModel`, but when I subclassed `class JsonModel(EndpointsModel)` (also did some other things in code, so can't be sure what exactly is happening) it started working. I reverted back to `EndpointsModel` and it's still workig (; Guess the only way to deal with thiss kind of issues is to turn everything else off and isolate modules with problems... – Sasxa Feb 24 '16 at 18:47

1 Answers1

1

Subclass class JsonModel(EndpointsModel) to make it start working again.

J_H
  • 17,926
  • 4
  • 24
  • 44