News for hackers. Stuff that matters.
Re:OAuth 3 by golergka on Thursday October 22, 2020 @08:40AM (#24857357)
> XYZ’s protocol is not just based on JSON, but it’s based on a particular technique known as polymorphic JSON. In this, a single field could have a different data type in different circumstances. For example, the resources field can be an array when requesting a single access token, or it can be an object when requesting multiple named access tokens. The difference in type indicates a difference in processing from the server. Within the resources array, each element can be either an object, representing a rich resource request description, or a string, representing a resource handle (scope).

This is horrible.

Re:OAuth 3 by pwinnski on Thursday October 22, 2020 @09:24AM (#24857727)
This is definitely not a protocol dreamed up by, say, Java developers.

Obviously it's doable in Java, but I'm hard-pressed to imagine a Java developer would think of such a thing.

Re:OAuth 3 by mumblemumble on Thursday October 22, 2020 @10:11AM (#24858210)
The only time I've ever encountered an API that used it extensively, it was done by a company that does all their implementation in Java.

My best guess at what happened, based on the shape of the API, is that they implemented it by taking their pre-existing domain model, which had a fairly deep subclassing hierarchy, liberally sprinkled some annotations from com.fasterxml.jackson.annotation, and dumped the result straight onto the wire.

You could absolutely do an object-oriented codebase where two different subclasses have fields with the same name and different types, and, depending on how you structure your code, it might not be too painful. And it's fairly easy to imagine someone serializing a structure like that to JSON without ever meditating on the fact that JSON won't retain the all the type information.

Ironically, the end result was an API that is nigh-impossible to consume from Java. I ended up writing a façade in Python.

I've also seen little bits of this happen in the internal APIs at my current company. Also a Java shop, also a result of trying to directly connect an internal object model to the API. I've never seen it done in an API implemented in a dynamic or functional language.

Re:OAuth 3 by pwinnski on Thursday October 22, 2020 @11:34AM (#24859117)
I hadn't considered the one-way nature of dumping Java classes to JSON.

Mainly because I work with classes that are serialized both to and from JSON, rather than having Jackson annotations added much later.

I can see it now. Horrifying! I'm sorry for your troubles.

Re:OAuth 3 by mumblemumble on Thursday October 22, 2020 @11:49AM (#24859312)
My take-away is this: Protocols should always be defined independently of any existing code.

The code-first approach only works well when you're doing something self-contained. Which, an API, almost by definition, is not.

Re:OAuth 3 by pwinnski on Thursday October 22, 2020 @02:09PM (#24861189)
I generally try to design APIs from a consumer perspective, and then usually I end up with something RESTful. Let the server do whatever it has to do, you know?
Re:OAuth 3 by tasogare on Thursday October 22, 2020 @09:35AM (#24857803)
This is a few magnitude even more infuriating than a spec I have to work with at work, where some keys are required to be URI...
Re:OAuth 3 by golergka on Thursday October 22, 2020 @11:20AM (#24858947)
I work in Typescript, which is arguably the best mainstream language suited to type such things, and still. This is insane.
Re:OAuth 3 by alyandon on Thursday October 22, 2020 @10:02AM (#24858118)
Lovely - I can't wait to see how horrible something like that is to implement in a language like Go.
Re:OAuth 3 by dullgiulio on Thursday October 22, 2020 @01:01PM (#24860266)
You have to implement UnmarshalJSON. Between each attempt to deserialize into a possible struct, be careful to return on errors that are not JSON serialisation errors (for example caused by reading from the underlying Reader, etc.)

It's ugly and verbose but there is no need to use empty interface.

Re:OAuth 3 by CameronNemo on Thursday October 22, 2020 @11:57AM (#24859394)
Sounds like a golang problem. Rust can easily handle it with union types from what I have seen.
Re:OAuth 3 by meddlepal on Thursday October 22, 2020 @11:00AM (#24858719)
I hope you enjoy working with interface{}

_sigh_

Re:OAuth 3 by nullandvoid on Thursday October 22, 2020 @10:17AM (#24858268)
interface{} to the rescue