Endpoints API - protorpc validation error
Asked Answered
A

1

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.

Ayah answered 21/2, 2016 at 18:39 Comment(4)
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?Infeld
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.Ayah
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.Infeld
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...Ayah
L
1

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

Langtry answered 1/1, 2018 at 20:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.