Giter VIP home page Giter VIP logo

Comments (9)

kwood avatar kwood commented on July 16, 2024

Definitely should expose the display strings in the API, for the same reason you should always use the constants to assign to them (never use integer literals!)

  1. We should use string literals, both for read and write, and they should be properly validated in the serializer
  2. Documentation, this should be possible to document automatically I would think.
  3. Ideally adding another item to the choice field in the model should be all you need to do. Serializer and documentation should pick that up automatically. If it's not possible out of the box, it should be trivial to write such a serializer

from http-api-design.

tboyce12 avatar tboyce12 commented on July 16, 2024

I like the idea of passing choice fields as an integer instead of a string because passing around integers is less prone to subtle errors such as capitalization, string encoding, spelling, etc. Just make sure to document the possible choices and corresponding integer values.

from http-api-design.

kwood avatar kwood commented on July 16, 2024

https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py#L970 <-- Looks like the ChoiceField serializer should do this out of the box

from http-api-design.

Leland-Takamine avatar Leland-Takamine commented on July 16, 2024

I think human readable values make more sense.

@tboyce12 On the contrary I think integers are more prone to errors as integers by themselves have no meaning.

re: Exposing choices
I don't see how choice parameters differ from any other part of an API spec. In other words, I think we should document the spec in the same way we document all other requirements of the API. If you are looking a more robust way of enforcing type-safety (as opposed to relying on documentation), I think the only other option would be Protocol Buffers (https://code.google.com/p/protobuf/), which I don't think are used by anyone besides Google and Square.

from http-api-design.

alanhogan avatar alanhogan commented on July 16, 2024

Definitely should expose the display strings in the API, for the same reason you should always use the constants to assign to them

Strongly agree — huge advantage of JSON as an API format is readability and I just can’t see any reason whatsoever to obfuscate meaning in API design.

I like the idea of passing choice fields as an integer instead of a string because passing around integers is less prone to subtle errors such as capitalization, string encoding, spelling, etc.

Easily solved by server-side validation (for client errors) or by tests — you wouldn’t accept -999 just like you wouldn't accept mail, and you can assert that the field is male

from http-api-design.

alanhogan avatar alanhogan commented on July 16, 2024

Which code, used to retrieve a localized gender string based on the gender of the user as returned from the API, has a bug?

switch user.gender
  when 1 then 'gender.male'
  when 2 then 'gender.female'
  else 'gender.unknown'

or

switch user.gender
  when 'female' then 'gender.male'
  when 'male' then 'gender.female'
  else 'gender.unknown'

(The correct answer is “definitely the second code block, and we have no way of knowing with the first code block without a reference”)

from http-api-design.

geemus avatar geemus commented on July 16, 2024

I almost always lean toward human-readable. I think any time (within reason) that you can do a small amount of extra work to transfer more context/info, you probably might as well. Additionally, I think this is a case where either the server can do a bit more work or EVERY client can do a bit more work. In those cases I usually feel that doing the work once/centrally is probably preferred.

In this case I think there is value in strings instead of integers to abstract away from the underlying stuff (in case you added new options and/or needed to change the meaning of existing values for some reason).

I'm not sure, off hand, of a good way to necessarily encode this info back to the user. In any documentation I would make it clear that there is an enum of valid options and clearly list them. Then in usage, I would raise an error (probably 422) if somebody posted one that didn't match. Ideally the body of that response could then clearly spell out something like "gender must be one of ...".

Hope that helps, happy to discuss further if it would be helpful though.

from http-api-design.

alanhogan avatar alanhogan commented on July 16, 2024

Excellent, well said. Thanks for chiming in, @geemus. (Looks like an R. Stevens avatar, yeah? Nice.)

from http-api-design.

geemus avatar geemus commented on July 16, 2024

@alanhogan hah, good eye. R. Stevens did in fact make me this fine avatar.

from http-api-design.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.