Skip to content

Instantly share code, notes, and snippets.

@jldiaz
Created June 9, 2015 15:04
Show Gist options
  • Save jldiaz/36a54dc6d09447b594ae to your computer and use it in GitHub Desktop.
Save jldiaz/36a54dc6d09447b594ae to your computer and use it in GitHub Desktop.
GAE endpoints problem with integers
application: your-app-id
version: 1
runtime: python27
threadsafe: true
api_version: 1
# Handlers tell App Engine how to route requests to your application.
handlers:
# This handler routes requests to your APIs to your Endpoints code.
# See https://developers.google.com/appengine/docs/python/endpoints/
- url: /_ah/spi/.*
script: example.app
secure: always
libraries:
- name: endpoints
version: 1.0
import endpoints
from protorpc import messages
from protorpc import message_types
from protorpc import remote
package = 'Example'
class Number(messages.Message):
number = messages.IntegerField(1)
@endpoints.api(name='example', version='v1')
class ExampleApi(remote.Service):
@endpoints.method(message_types.VoidMessage, Number,
path = "getNumber", http_method='GET', name = 'getNumber')
def get_number(self, _):
return Number(number = 123) # This is an integer. Isn't?
app = endpoints.api_server([ExampleApi])

When the API endpoint is invoked via the API explorer, or simply via:

$ curl http://localhost:8080//_ah/api/example/v1/getNumber

The following JSON is received

{
 "number": "123"
}

Why is "123" a string? Since the message type was declared as IntegerField() I expected it to be an integer instead, i.e. I expected the following JSON:

{
 "number": 123
}
@hello-josh
Copy link

Setting the variant to a 32 bit integer type fixes the issue.

class Number(messages.Message):
    number = messages.IntegerField(1, variant=messages.Variant.INT32)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment