|
| 1 | +<!DOCTYPE html> |
| 2 | +<html> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8"> |
| 5 | + <title>JSON:API Extension for OpenAPI </title> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 7 | + <meta name="title" content="JSON:API Extension for OpenAPI "> |
| 8 | + |
| 9 | + |
| 10 | + <link rel="icon" href="../../favicon.ico"> |
| 11 | + <link rel="stylesheet" href="../../public/docfx.min.css"> |
| 12 | + <link rel="stylesheet" href="../../public/main.css"> |
| 13 | + <meta name="docfx:navrel" content="../../toc.html"> |
| 14 | + <meta name="docfx:tocrel" content="../../toc.html"> |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + <meta name="docfx:docurl" content="https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/docs/ext/openapi/index.md/#L1"> |
| 20 | + <meta name="loc:inThisArticle" content="In this article"> |
| 21 | + <meta name="loc:searchResultsCount" content="{count} results for "{query}""> |
| 22 | + <meta name="loc:searchNoResults" content="No results for "{query}""> |
| 23 | + <meta name="loc:tocFilter" content="Filter by title"> |
| 24 | + <meta name="loc:nextArticle" content="Next"> |
| 25 | + <meta name="loc:prevArticle" content="Previous"> |
| 26 | + <meta name="loc:themeLight" content="Light"> |
| 27 | + <meta name="loc:themeDark" content="Dark"> |
| 28 | + <meta name="loc:themeAuto" content="Auto"> |
| 29 | + <meta name="loc:changeTheme" content="Change theme"> |
| 30 | + <meta name="loc:copy" content="Copy"> |
| 31 | + <meta name="loc:downloadPdf" content="Download PDF"> |
| 32 | + |
| 33 | + <script type="module" src="./../../public/docfx.min.js"></script> |
| 34 | + |
| 35 | + <script> |
| 36 | + const theme = localStorage.getItem('theme') || 'auto' |
| 37 | + document.documentElement.setAttribute('data-bs-theme', theme === 'auto' ? (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') : theme) |
| 38 | + </script> |
| 39 | + |
| 40 | + </head> |
| 41 | + |
| 42 | + <body class="tex2jax_ignore" data-layout="" data-yaml-mime=""> |
| 43 | + <header class="bg-body border-bottom"> |
| 44 | + <nav id="autocollapse" class="navbar navbar-expand-md" role="navigation"> |
| 45 | + <div class="container-xxl flex-nowrap"> |
| 46 | + <a class="navbar-brand" href="../../index.html"> |
| 47 | + <img id="logo" class="svg" src="../../logo.svg" alt=""> |
| 48 | + |
| 49 | + </a> |
| 50 | + <button class="btn btn-lg d-md-none border-0" type="button" data-bs-toggle="collapse" data-bs-target="#navpanel" aria-controls="navpanel" aria-expanded="false" aria-label="Toggle navigation"> |
| 51 | + <i class="bi bi-three-dots"></i> |
| 52 | + </button> |
| 53 | + <div class="collapse navbar-collapse" id="navpanel"> |
| 54 | + <div id="navbar"> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + </div> |
| 58 | + </nav> |
| 59 | + </header> |
| 60 | + |
| 61 | + <main class="container-xxl"> |
| 62 | + |
| 63 | + <div class="content"> |
| 64 | + <div class="actionbar"> |
| 65 | + |
| 66 | + <nav id="breadcrumb"></nav> |
| 67 | + </div> |
| 68 | + |
| 69 | + <article data-uid=""> |
| 70 | +<h1 id="jsonapi-extension-for-openapi">JSON:API Extension for OpenAPI</h1> |
| 71 | + |
| 72 | +<p>This extension facilitates using OpenAPI client generators targeting JSON:API documents.</p> |
| 73 | +<p>In JSON:API, a resource object contains the <code>type</code> member, which defines the structure of nested <a href="https://jsonapi.org/format/#document-resource-object-attributes">attributes</a> and <a href="https://jsonapi.org/format/#document-resource-object-relationships">relationships</a> objects. |
| 74 | +While OpenAPI supports such constraints using <code>allOf</code> inheritance with a discriminator property for the <code>data</code> member, |
| 75 | +it provides no way to express that the discriminator recursively applies to nested objects.</p> |
| 76 | +<p>This extension addresses that limitation by defining additional discriminator properties to guide code generation tools.</p> |
| 77 | +<h2 id="uri">URI</h2> |
| 78 | +<p>This extension has the URI <code>https://www.jsonapi.net/ext/openapi</code>. |
| 79 | +Because code generators often choke on the double quotes in <code>Accept</code> and <code>Content-Type</code> HTTP header values, a relaxed form is also permitted: <code>openapi</code>.</p> |
| 80 | +<p>For example, the following <code>Content-Type</code> header:</p> |
| 81 | +<pre><code class="lang-http">Content-Type: application/vnd.api+json; ext="https://www.jsonapi.net/ext/openapi" |
| 82 | +</code></pre> |
| 83 | +<p>is equivalent to:</p> |
| 84 | +<pre><code class="lang-http">Content-Type: application/vnd.api+json; ext=openapi |
| 85 | +</code></pre> |
| 86 | +<p>To avoid the need for double quotes when multiple extensions are used, the following relaxed form can be used:</p> |
| 87 | +<pre><code class="lang-http">Content-Type: application/vnd.api+json; ext=openapi; ext=atomic |
| 88 | +</code></pre> |
| 89 | +<div class="NOTE"> |
| 90 | +<h5>Note</h5> |
| 91 | +<p>The <a href="https://jsonapi.org/format/#media-type-parameter-rules">base specification</a> <em>forbids</em> the use of multiple <code>ext</code> parameters |
| 92 | +and <em>requires</em> that each extension name must be a URI. |
| 93 | +This extension relaxes both constraints for practical reasons, to workaround bugs in client generators that produce broken code otherwise.</p> |
| 94 | +</div> |
| 95 | +<h2 id="namespace">Namespace</h2> |
| 96 | +<p>This extension uses the namespace <code>openapi</code>.</p> |
| 97 | +<div class="NOTE"> |
| 98 | +<h5>Note</h5> |
| 99 | +<p>JSON:API extensions can only introduce new document members using a reserved namespace as a prefix.</p> |
| 100 | +</div> |
| 101 | +<h2 id="document-structure">Document Structure</h2> |
| 102 | +<p>A document that supports this extension MAY include any of the top-level members allowed by the base specification, |
| 103 | +including any members defined in the <a href="https://jsonapi.org/ext/atomic/">Atomic Operations extension</a>.</p> |
| 104 | +<h3 id="resource-objects">Resource Objects</h3> |
| 105 | +<p>In addition to the members allowed by the base specification, the following member MAY be included |
| 106 | +in <a href="https://jsonapi.org/format/#document-resource-object-attributes">attributes</a> and <a href="https://jsonapi.org/format/#document-resource-object-relationships">relationships</a> objects:</p> |
| 107 | +<ul> |
| 108 | +<li><code>openapi:discriminator</code> - A string that MUST be identical to the <code>type</code> member in the containing <a href="https://jsonapi.org/format/#document-resource-objects">resource object</a>.</li> |
| 109 | +</ul> |
| 110 | +<p>Here's how an article (i.e. a resource of type "articles") might appear in a document:</p> |
| 111 | +<pre><code class="lang-json">{ |
| 112 | + "data": { |
| 113 | + "type": "articles", |
| 114 | + "id": "1", |
| 115 | + "attributes": { |
| 116 | + "openapi:discriminator": "articles", |
| 117 | + "title": "Rails is Omakase" |
| 118 | + }, |
| 119 | + "relationships": { |
| 120 | + "openapi:discriminator": "articles", |
| 121 | + "author": { |
| 122 | + "data": { "type": "people", "id": "9" } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | +</code></pre> |
| 128 | +<h3 id="atomic-operations">Atomic Operations</h3> |
| 129 | +<p>In addition to the members allowed by the <a href="https://jsonapi.org/ext/atomic/">Atomic Operations extension</a>, |
| 130 | +the following member MAY be included in elements of an <code>atomic:operations</code> array:</p> |
| 131 | +<ul> |
| 132 | +<li><code>openapi:discriminator</code> - A free-format string to facilitate generation of client code.</li> |
| 133 | +</ul> |
| 134 | +<p>For example:</p> |
| 135 | +<pre><code class="lang-http">POST /operations HTTP/1.1 |
| 136 | +Host: example.org |
| 137 | +Content-Type: application/vnd.api+json; ext="https://www.jsonapi.net/ext/openapi https://jsonapi.org/ext/atomic" |
| 138 | +Accept: application/vnd.api+json; ext="https://www.jsonapi.net/ext/openapi https://jsonapi.org/ext/atomic" |
| 139 | + |
| 140 | +{ |
| 141 | + "atomic:operations": [{ |
| 142 | + "openapi:discriminator": "add-article", |
| 143 | + "op": "add", |
| 144 | + "data": { |
| 145 | + "type": "articles", |
| 146 | + "attributes": { |
| 147 | + "openapi:discriminator": "articles", |
| 148 | + "title": "JSON API paints my bikeshed!" |
| 149 | + } |
| 150 | + } |
| 151 | + }] |
| 152 | +} |
| 153 | +</code></pre> |
| 154 | +<h2 id="processing">Processing</h2> |
| 155 | +<p>A server MAY ignore the <code>openapi:discriminator</code> member in <a href="https://jsonapi.org/format/#document-resource-object-attributes">attributes</a> and <a href="https://jsonapi.org/format/#document-resource-object-relationships">relationships</a> objects from incoming requests. |
| 156 | +A server SHOULD ignore the <code>openapi:discriminator</code> member in elements of an <code>atomic:operations</code> array.</p> |
| 157 | +<p>A server MUST include the <code>openapi:discriminator</code> member in <a href="https://jsonapi.org/format/#document-resource-object-attributes">attributes</a> and <a href="https://jsonapi.org/format/#document-resource-object-relationships">relationships</a> objects in outgoing responses. |
| 158 | +The member value MUST be the same as the <code>type</code> member value of the containing resource object.</p> |
| 159 | +<p>A client MAY include the <code>openapi:discriminator</code> member in <a href="https://jsonapi.org/format/#document-resource-object-attributes">attributes</a> and <a href="https://jsonapi.org/format/#document-resource-object-relationships">relationships</a> objects in outgoing requests. |
| 160 | +The member value MUST be the same as the <code>type</code> member value of the containing resource object.</p> |
| 161 | +<p>A client MAY include the <code>openapi:discriminator</code> member in elements of an <code>atomic:operations</code> array.</p> |
| 162 | +<h3 id="processing-errors">Processing Errors</h3> |
| 163 | +<p>A server SHOULD validate that the value of the <code>openapi:discriminator</code> member in |
| 164 | +<a href="https://jsonapi.org/format/#document-resource-object-attributes">attributes</a> and <a href="https://jsonapi.org/format/#document-resource-object-relationships">relationships</a> objects |
| 165 | +is identical to the <code>type</code> member in the containing resource object. When validation fails, the server MUST respond with a <code>409 Conflict</code> |
| 166 | +and SHOULD include a document with a top-level <code>errors</code> member that contains an error object.</p> |
| 167 | + |
| 168 | +</article> |
| 169 | + |
| 170 | + <div class="contribution d-print-none"> |
| 171 | + <a href="https://github.com/json-api-dotnet/JsonApiDotNetCore/blob/master/docs/ext/openapi/index.md/#L1" class="edit-link">Edit this page</a> |
| 172 | + </div> |
| 173 | + |
| 174 | + <div class="next-article d-print-none border-top" id="nextArticle"></div> |
| 175 | + |
| 176 | + </div> |
| 177 | + |
| 178 | + <div class="affix"> |
| 179 | + <nav id="affix"></nav> |
| 180 | + </div> |
| 181 | + </main> |
| 182 | + |
| 183 | + |
| 184 | + <footer class="border-top text-secondary"> |
| 185 | + <div class="container-xxl"> |
| 186 | + <div class="flex-fill"> |
| 187 | + <span>Made with <a href="https://dotnet.github.io/docfx">docfx</a></span> |
| 188 | + </div> |
| 189 | + </div> |
| 190 | + </footer> |
| 191 | + </body> |
| 192 | +</html> |
0 commit comments