<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Umar Tahir</title>
    <description>The latest articles on DEV Community by Umar Tahir (@umartahir93).</description>
    <link>https://dev.to/umartahir93</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F895107%2F6aa1ef2a-728d-4852-9f23-a1928044319c.jpeg</url>
      <title>DEV Community: Umar Tahir</title>
      <link>https://dev.to/umartahir93</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/umartahir93"/>
    <language>en</language>
    <item>
      <title>`allOf` vs `oneOf` vs `anyOf` in OpenAPI (with examples)</title>
      <dc:creator>Umar Tahir</dc:creator>
      <pubDate>Tue, 23 Dec 2025 07:20:24 +0000</pubDate>
      <link>https://dev.to/umartahir93/oneof-vs-anyof-vs-allof-in-openapi-with-real-examples-93h</link>
      <guid>https://dev.to/umartahir93/oneof-vs-anyof-vs-allof-in-openapi-with-real-examples-93h</guid>
      <description>&lt;p&gt;If you’ve worked with OpenAPI schemas, you’ve probably seen &lt;code&gt;oneOf&lt;/code&gt;, &lt;code&gt;anyOf&lt;/code&gt;, and &lt;code&gt;allOf&lt;/code&gt; and wondered:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“They look similar… so when should I actually use each one?”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s break them down with &lt;strong&gt;practical, API-design–focused examples&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;allOf&lt;/code&gt; &lt;strong&gt;Composition / Inheritance&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Meaning:&lt;/strong&gt;&lt;br&gt;
👉 The schema must satisfy &lt;strong&gt;all&lt;/strong&gt; listed schemas.&lt;/p&gt;

&lt;p&gt;Think of &lt;code&gt;allOf&lt;/code&gt; as &lt;strong&gt;“AND”&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  When to use it
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;To &lt;strong&gt;compose schemas&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;To model &lt;strong&gt;base + extension&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;InverterCreate&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;allOf&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;$ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#/components/schemas/ApplianceBase'&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;object&lt;/span&gt;
      &lt;span class="na"&gt;properties&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
        &lt;span class="na"&gt;reverseFlow&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
          &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;boolean&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;What this means:&lt;/strong&gt;&lt;br&gt;
✔ The request must match &lt;code&gt;ApplianceBase&lt;/code&gt;&lt;br&gt;
✔ AND it must match other specific fields&lt;/p&gt;

&lt;p&gt;This is perfect for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Base appliance fields (&lt;code&gt;name&lt;/code&gt;, &lt;code&gt;location&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Plus type-specific fields&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;oneOf&lt;/code&gt; &lt;strong&gt;Exactly one schema&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Meaning:&lt;/strong&gt;&lt;br&gt;
👉 The schema must match &lt;strong&gt;exactly one&lt;/strong&gt; of the listed schemas.&lt;/p&gt;

&lt;p&gt;Think of &lt;code&gt;oneOf&lt;/code&gt; as &lt;strong&gt;“XOR”&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  When to use it
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When &lt;strong&gt;schemas are mutually exclusive&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;When you want &lt;strong&gt;strict validation&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Often combined with a &lt;strong&gt;discriminator&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;oneOf&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;$ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#/components/schemas/InverterPatch'&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;$ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#/components/schemas/EVPatch'&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;$ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#/components/schemas/EVCSPatch'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Important detail ⚠️
&lt;/h3&gt;

&lt;p&gt;If a request matches &lt;strong&gt;more than one schema&lt;/strong&gt;, validation &lt;strong&gt;fails&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That’s why &lt;code&gt;oneOf&lt;/code&gt; + PATCH + overlapping fields can be tricky.&lt;/p&gt;
&lt;h3&gt;
  
  
  With discriminator (recommended)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;discriminator&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;propertyName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;type&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"inverter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"reverseFlow"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The discriminator tells OpenAPI &lt;strong&gt;which schema to use&lt;/strong&gt;, removing ambiguity.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;anyOf&lt;/code&gt; &lt;strong&gt;One or more schemas&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Meaning:&lt;/strong&gt;&lt;br&gt;
👉 The schema must match &lt;strong&gt;at least one&lt;/strong&gt; of the listed schemas.&lt;/p&gt;

&lt;p&gt;Think of &lt;code&gt;anyOf&lt;/code&gt; as &lt;strong&gt;“OR”&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  When to use it
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When schemas &lt;strong&gt;overlap&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;When flexibility is more important than strictness&lt;/li&gt;
&lt;li&gt;When multiple schema matches are acceptable&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;anyOf&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;$ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#/components/schemas/InverterSpecificPatch'&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;$ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#/components/schemas/EVSpecificPatch'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This allows requests that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Match one schema&lt;/li&gt;
&lt;li&gt;Match multiple schemas&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  PATCH + Overlapping Fields: The Key Insight
&lt;/h2&gt;

&lt;p&gt;Consider this PATCH request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"reverseFlow"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;reverseFlow&lt;/code&gt; exists in &lt;strong&gt;multiple schemas&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You’re using &lt;code&gt;oneOf&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 OpenAPI &lt;strong&gt;cannot decide which schema applies&lt;/strong&gt;&lt;br&gt;
👉 Validation fails &lt;strong&gt;unless you use a discriminator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That’s why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;oneOf&lt;/code&gt; + PATCH + overlapping fields → discriminator strongly recommended&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;allOf&lt;/code&gt; is great for base + extensions&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;code&gt;anyOf&lt;/code&gt; is useful when overlap is intentional and acceptable&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Quick decision table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Goal&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Schema composition / inheritance&lt;/td&gt;
&lt;td&gt;&lt;code&gt;allOf&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Exactly one valid schema&lt;/td&gt;
&lt;td&gt;&lt;code&gt;oneOf&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flexible matching&lt;/td&gt;
&lt;td&gt;&lt;code&gt;anyOf&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PATCH with overlapping fields&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;oneOf&lt;/code&gt; + discriminator &lt;strong&gt;or&lt;/strong&gt; single schema&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Strict API contracts&lt;/td&gt;
&lt;td&gt;&lt;code&gt;oneOf&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Simpler client experience&lt;/td&gt;
&lt;td&gt;Single schema / &lt;code&gt;anyOf&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  Final takeaway
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;allOf&lt;/code&gt;&lt;/strong&gt; = build schemas&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;oneOf&lt;/code&gt;&lt;/strong&gt; = enforce exclusivity&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;anyOf&lt;/code&gt;&lt;/strong&gt; = allow flexibility&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Discriminators exist to remove ambiguity&lt;/strong&gt;, especially for PATCH requests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your API has &lt;strong&gt;polymorphic resources&lt;/strong&gt; and &lt;strong&gt;partial updates&lt;/strong&gt;, understanding this distinction is crucial to designing clean, predictable OpenAPI specs.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>api</category>
      <category>design</category>
      <category>backend</category>
    </item>
    <item>
      <title>Understanding oneOf in OpenAPI (Without the Confusion)</title>
      <dc:creator>Umar Tahir</dc:creator>
      <pubDate>Tue, 23 Dec 2025 07:00:22 +0000</pubDate>
      <link>https://dev.to/umartahir93/understanding-oneof-in-openapi-without-the-confusion-3cp6</link>
      <guid>https://dev.to/umartahir93/understanding-oneof-in-openapi-without-the-confusion-3cp6</guid>
      <description>&lt;p&gt;When designing APIs with OpenAPI, &lt;code&gt;oneOf&lt;/code&gt; often looks intimidating. It’s powerful, but easy to misuse especially with PATCH endpoints. Let’s break it down simply.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;oneOf&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;In OpenAPI (JSON Schema), &lt;code&gt;oneOf&lt;/code&gt; means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The request body must match exactly one schema from the list.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Not zero.&lt;br&gt;
Not two.&lt;br&gt;
&lt;strong&gt;Exactly one.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;schema&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;oneOf&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;$ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#/components/schemas/InverterPatch'&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;$ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#/components/schemas/EVPatch'&lt;/span&gt;
    &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;$ref&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'&lt;/span&gt;&lt;span class="s"&gt;#/components/schemas/EVCSPatch'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells OpenAPI:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“The payload must conform to exactly one of these schemas.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why &lt;code&gt;oneOf&lt;/code&gt; is tricky with PATCH
&lt;/h2&gt;

&lt;p&gt;PATCH requests are &lt;strong&gt;partial updates&lt;/strong&gt;. Clients often send only a few fields.&lt;/p&gt;

&lt;p&gt;Now imagine this payload:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"reverseFlow"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;reverseFlow&lt;/code&gt; exists in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;InverterPatch&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;EVCSPatch&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then this payload matches &lt;strong&gt;more than one schema&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;🚫 Result: &lt;strong&gt;Validation fails&lt;/strong&gt;, because &lt;code&gt;oneOf&lt;/code&gt; requires &lt;em&gt;exactly one match&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Enter the Discriminator
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;discriminator&lt;/strong&gt; solves this ambiguity.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;discriminator&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;propertyName&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;type&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the client sends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"inverter"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"reverseFlow"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;OpenAPI immediately knows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;type = inverter&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;→ use &lt;code&gt;InverterPatch&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;→ validation succeeds&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;💡 The discriminator tells OpenAPI &lt;em&gt;which schema to validate against&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  When You SHOULD Use &lt;code&gt;oneOf&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Use &lt;code&gt;oneOf&lt;/code&gt; (with a discriminator) when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have &lt;strong&gt;multiple schemas&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Some fields &lt;strong&gt;overlap&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Clients may send &lt;strong&gt;partial updates&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You want &lt;strong&gt;OpenAPI-level validation&lt;/strong&gt;, not just backend logic&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When You Should NOT Use &lt;code&gt;oneOf&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Avoid &lt;code&gt;oneOf&lt;/code&gt; if:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You don’t need OpenAPI to pick between schemas&lt;/li&gt;
&lt;li&gt;Backend already knows the resource type&lt;/li&gt;
&lt;li&gt;You’re okay validating type-specific rules at runtime&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In those cases, a &lt;strong&gt;single PATCH schema with optional fields&lt;/strong&gt; is simpler and often better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Takeaway
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;oneOf&lt;/code&gt; = &lt;strong&gt;exactly one schema must match&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Overlapping fields + PATCH = ambiguity&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Discriminator resolves ambiguity&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Don’t use &lt;code&gt;oneOf&lt;/code&gt; unless you actually need schema selection&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Used correctly, &lt;code&gt;oneOf&lt;/code&gt; makes your API &lt;strong&gt;safer and self-documenting&lt;/strong&gt;. Used incorrectly, it makes PATCH endpoints painful.&lt;/p&gt;

</description>
      <category>api</category>
      <category>architecture</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
