google-cloud-endpointsendpoints-proto-datastore

endpoints-proto-datastore - field should be required on POST but not GET


Let's say I have a Model with two mandatory fields:

class ExampleModel(EndpointsModel):
    attr1 = ndb.StringProperty(required=True)
    attr2 = ndb.StringProperty(required=True)

Then I want to use endpoints-proto-datastore to query on either attr1 or attr2:

@ExampleModel.query_method(query_fields=('attr1', 'attr2'),
                           path='example', name='list')
    def example_list(self, query):
        return query

This fails if I only provide one of the fields - from API Explorer it's a required field, but the API itself returns:

{
 "error": {
  "code": 400, 
  "errors": [
   {
    "domain": "global", 
    "message": "Error parsing ProtoRPC request (Unable to parse request content: Message CombinedContainer is missing required field attr2)", 
    "reason": "badRequest"
   }
  ], 
  "message": "Error parsing ProtoRPC request (Unable to parse request content: Message CombinedContainer is missing required field attr2)"
 }
}

Obviously I could mark them as not required, then handle the check within the application code - but I was wondering if someone else had come up with a better solution.

Many thanks


Solution

  • This is an old question but I ran into the same confusion. This was the answer I found. Basically if you want to make something mandated on Post but not get you need to make a custom proto class. Which can only be used with method and not query_method.