TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Swagger/OpenAPI examples are bad, like reaaalllyy bad

28 点作者 d3nigma将近 4 年前
While reading about the OpenAPI spec, I found these examples for &quot;good&quot; APIs:<p>- https:&#x2F;&#x2F;petstore.swagger.io&#x2F; (older version)<p>- https:&#x2F;&#x2F;petstore3.swagger.io&#x2F;<p>However, imho they are really bad examples as they do not follow basic principles of good API design. Here are some reasons:<p>1. They use &quot;&#x2F;pet&quot; instead of &quot;&#x2F;pets&quot; for the pet collection (Besides the fact that &quot;&#x2F;pets&quot; isn&#x27;t even an endpoint and you can not get a list of all pets ^^).<p>2. The login &amp; logout endpoints are under &#x2F;user (i.e. &#x2F;user&#x2F;login, &#x2F;user&#x2F;logout)<p>3. You do not use PUT on a collection as they did with PUT &#x2F;pet<p>4. You do not use POST as they did with &#x2F;pet&#x2F;{pet_id} to update a single resource.<p>5. You do not use POST to create a resource and PUT to update the same resource as they did with PUT &#x2F;pet and POST &#x2F;pet<p>Some might argue that it is a matter of opinion that these examples are bad. You may be right, but at minimum an API should be self-consistent. But even that is not the case here.<p>That&#x27;s why I&#x27;m wondering what this is all about. Do I not know the latest API design principles yet or why are the examples for a de facto industry standard so lousy?<p>And btw what is your opinion on the API design of the examples?

7 条评论

lhorie将近 4 年前
It feels like you&#x27;re attacking a design without understanding the design principles behind it.<p>Looking at <a href="https:&#x2F;&#x2F;petstore3.swagger.io" rel="nofollow">https:&#x2F;&#x2F;petstore3.swagger.io</a>, the API is `&#x2F;pet` (singular), because that&#x27;s exactly what it represents: the type of a singular pet. This design choice has to do with orthogonality: for example, what is `POST &#x2F;pets` supposed to do? Create a new list of pets? That makes no sense. If you want to argue that it should create a new pet, then that makes no sense either, <i>because the endpoint is explicitly about a collection of pets</i>. This distinction may seem like nitpicking, but consider an entity type of ambiguous plurality like `&#x2F;group`. You want to be clear about whether an action on this endpoint applies to a `group` or to individual entities in the group and you want that semantic to be consistent with other endpoints.<p>On the same vein about orthogonality, `&#x2F;user&#x2F;{action}` is orthogonal to `&#x2F;pet&#x2F;{action}` in the sense that they both follow the same OOP-ish pattern. Calling an endpoint merely `&#x2F;login` doesn&#x27;t fit into that mold (e.g. Can a pet login? Why is `&#x2F;login` floating without a noun, but `DELETE &#x2F;user&#x2F;{id}` isn&#x27;t? etc)<p>The choices of PUT and POST have to do with what the HTTP spec says about idempotency. `PUT &#x2F;pet` must be idempotent (i.e. upsert a pet), `POST &#x2F;pet` is not idempotent (i.e. create a pet).<p>`POST &#x2F;pet&#x2F;{id}` is a bit of a seemingly counterintuitive one, but again it has to do with idempotency. It&#x27;s actually a deliberate choice to indicate that there may be internal logic that isn&#x27;t pure (for example, there may be a lastModified field).
lmilcin将近 4 年前
I think you are overreacting.<p>Even if you were right (which I don&#x27;t agree is the case) you need to understand the examples are not there to teach you good API design.<p>The Pet Store is a Hello World of APIs and the point of this example is to replicate an example that is already known to people from other frameworks&#x2F;libraries.<p>The point is to show OpenAPI on an example that is, hopefully, already familiar to the user.<p>It is as if you were arguing how Hello World! should be capitalized or whether there should be a comma in it or not.
colpabar将近 4 年前
&gt;<i>Do I not know the latest API design principles yet or why are the examples for a de facto industry standard so lousy?</i><p>Swagger&#x2F;OpenAPI is not really a standard, it&#x27;s just a way to document an API. JsonAPI [1] is an example of a standard.<p>I only have a few years of API development under my belt, but something I have learned is that consumers of APIs really don&#x27;t care about any of the points you listed. No one really cares if your API does not strictly follow ReST principles. They care that it works, that it&#x27;s fast, and that it&#x27;s well documented, in that order. Sure, using all the different HTTP verbs feels nice as a developer, but you know what is much simpler for everyone? GET for all reads and POST for all writes.<p>To be clear, I am not saying that you should give up on following best practices in your APIs. What I am saying is that you should design your APIs to meet your clients&#x27; needs, not to satisfy &quot;principles of good API design.&quot;<p>[1]: <a href="https:&#x2F;&#x2F;jsonapi.org" rel="nofollow">https:&#x2F;&#x2F;jsonapi.org</a>
评论 #27955580 未加载
ggm将近 4 年前
Is this about the semantics and syntax of English? Would your response be different in a hypothetical language where the pluralistic pet is (s)pet? How about if the plurality of pet was &quot;scront&quot; or &quot;pet, uncountable many&quot;<p>Mapping English words with &#x2F; as a separator and into a world of 4 or 6 action verbs is a huge constraint on &quot;meaning&quot; in the linguistic sense.
d--b将近 4 年前
Your points may be valid, but do these points qualify as “like reaaalllyy bad”, of course not.<p>We’re quite a few to think that REST api design principles are silly, dand that people who impose their view &#x2F; mock others for not following them strictly are mindless zealots.
CodeWriter23将近 4 年前
You appear to be relying on a working example intended as a visualization tool to go alongside some really dry reference documentation as a guide for good API design.
chasingthewind将近 4 年前
I agree with you 100%. There are very good and valuable reasons to follow some of the purist “rules” around designing REST APIs. It makes them significantly easier to consume when they don’t violate the principle of least astonishment.