question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Decimal fields conveyed as strings in the REST API

See original GitHub issue

NetBox version

v2.11.2

Python version

3.9

Steps to Reproduce

The HTTP GET response returns “vcpu” argument as string, but it should be a float, as requested on Issue #5975.

Steps to reproduce:

  1. Make HTTP GET request to /api/virtualization/virtual-machines/{id}/

HTTP GET Request

URI: https://demo.netbox.dev/api/virtualization/virtual-machines/543/

Headers: Authorization: Token 977e9fb09fbfb2aa67ae06f90655b52edcee01e5 Host: demo.netbox.dev User-Agent: PostmanRuntime/7.28.0 Accept: / Accept-Encoding: gzip, deflate, br Connection: keep-alive Content-Type: application/json

Expected Behavior

HTTP GET Response

{
    "id": 543,
    "url": "https://demo.netbox.dev/api/virtualization/virtual-machines/543/",
    "display": "vcpu is string, not float",
    "name": "vcpu is string, not float",
    "status": {
        "value": "active",
        "label": "Active"
    },
    "site": null,
    "cluster": {
        "id": 9,
        "url": "https://demo.netbox.dev/api/virtualization/clusters/9/",
        "display": "DO-AMS3",
        "name": "DO-AMS3"
    },
    "role": null,
    "tenant": null,
    "platform": null,
    "primary_ip": null,
    "primary_ip4": null,
    "primary_ip6": null,
    "vcpus": 10.00,
    "memory": 8196,
    "disk": 50,
    "comments": "",
    "local_context_data": null,
    "tags": [],
    "custom_fields": {},
    "config_context": {},
    "created": "2021-05-04",
    "last_updated": "2021-05-04T02:19:09.726988Z"
}

Observed Behavior

HTTP GET Response

{
    "id": 543,
    "url": "https://demo.netbox.dev/api/virtualization/virtual-machines/543/",
    "display": "vcpu is string, not float",
    "name": "vcpu is string, not float",
    "status": {
        "value": "active",
        "label": "Active"
    },
    "site": null,
    "cluster": {
        "id": 9,
        "url": "https://demo.netbox.dev/api/virtualization/clusters/9/",
        "display": "DO-AMS3",
        "name": "DO-AMS3"
    },
    "role": null,
    "tenant": null,
    "platform": null,
    "primary_ip": null,
    "primary_ip4": null,
    "primary_ip6": null,
    "vcpus": "10.00",
    "memory": 8196,
    "disk": 50,
    "comments": "",
    "local_context_data": null,
    "tags": [],
    "custom_fields": {},
    "config_context": {},
    "created": "2021-05-04",
    "last_updated": "2021-05-04T02:19:09.726988Z"
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jeremystretchcommented, May 5, 2021

I’m not sure it’s fair to classify this as a bug, since it is technically intended behavior, though I do think we’re okay to change it. Let’s tag this for v2.12.

0reactions
emersonfelipespcommented, May 6, 2021

In general, I have an opinion of never expect specific data types in responses. Given all APIs out there one thing that I have discovered is that you have to catch and format data anyway before processing. To be honest, life would be easier if everything was strings, and the processing of responses handle convversions.

But thats me, always encapsulating stuff in str(), int(), float() and so on…

Although your opinion makes sense and I had to do exactly what you said, this specific behavior in Netbox is very different from the rest of the system, so would be great to make things working as expected, as the system is so consistent.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why would you use a string in JSON to represent a decimal ...
The main reason to transfer numeric values in JSON as strings is to eliminate any loss of precision or ambiguity in transfer.
Read more >
json parse error converting string to decimal in rest api
Hi frnds, i have a class where json parameters come in string format among them two values come in decimal format i was...
Read more >
Representing price in response and request - Meta - JSON API
Strings I think are easier for handling, would be converted to decimal inside handler. But then there is missing validation -> price is...
Read more >
Zoned and Packed Decimal Fields as IDMS Keys - TechDocs
Zoned decimal fields use one byte of storage to represent each single digit within a value. The high-order nibble of the last byte...
Read more >
REST API Fields - Cybersource Technical Documentation Portal
Data Type: String ... REST API Field: acquirerInformation.country ... 2.x messages to be communicated between all components (in minutes).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found