title: Openapi how to avoid anyOf showing Null for optional response
body: <p>I have openapi response schema like this</p>
  <pre><code>ProfileMetrics:
    description: List of metrics available in Field Profile
    type: object
    properties:
      fieldProfileMetrics:
        type: array
        items:
          type: object
      summaryProfileMetrics:
        type: array
        items:
          type: object
    anyOf:
      - required: [ fieldProfileMetrics ]
      - required: [ summaryProfileMetrics ]
  </code></pre>
  <p>Which results in a Response for example where I don't want fieldProfileMetrics in the Response.</p>
  <pre><code>{
    &quot;fieldProfileMetrics&quot;: null,
    &quot;summaryProfileMetrics&quot;: [
      {
        &quot;name&quot;: &quot;count_events&quot;,
        &quot;description&quot;: &quot;Number of rows/events in the dataset for particular timeframe&quot;,
        &quot;subMetrics&quot;: []
      }]
  }
  </code></pre>
  <p>So the response can be either <code>{fieldProfileMetric : SomeValue}</code> , <code>{summaryProfileMetric : SomeValue}</code> or <code>{fieldProfileMetric : SomeValue, summaryProfileMetric : SomeValue}</code></p>
  <p>The Service Class is like this</p>
  <pre><code>@Service
  class ProfileMetricsService : ProfileMetricsApiService {
  
    override fun v1MetricsGet(profile: ProfileType?): ProfileMetrics {
      return when (profile) {
        ProfileType.field -&gt; convertToProfileMetrics(profile, getFieldProfileMetrics())
        ProfileType.summary -&gt; convertToProfileMetrics(
          profile, getSummaryProfileMetrics()
        )
        else -&gt; convertToProfileMetrics(
          getFieldProfileMetrics(),
          getSummaryProfileMetrics()
        )
      }
    }
  
    private fun getFieldProfileMetrics(): MutableList&lt;Map&lt;String, Serializable&gt;&gt; {
      val fieldProfileList = mutableListOf&lt;Map&lt;String, Serializable&gt;&gt;()
      FieldProfileMetrics.values().forEach { it -&gt;
        fieldProfileList.add(
          mapOf(
            Pair(&quot;name&quot;, it.value),
            Pair(FieldProfileMetrics::description.name, it.description),
            Pair(FieldProfileMetrics::subMetrics.name, it.subMetrics.map { it.value }.toTypedArray())
          )
        )
      }
      return fieldProfileList
    }
  
    private fun getSummaryProfileMetrics(): MutableList&lt;Map&lt;String, Serializable&gt;&gt; {
      val summaryProfileList = mutableListOf&lt;Map&lt;String, Serializable&gt;&gt;()
      SummaryProfileMetrics.values().forEach { profileMetric -&gt;
        summaryProfileList.add(
          mapOf(
            Pair(&quot;name&quot;, profileMetric.value),
            Pair(SummaryProfileMetrics::description.name, profileMetric.description),
            Pair(SummaryProfileMetrics::subMetrics.name, profileMetric.subMetrics.map { it.value }.toTypedArray())
          )
        )
      }
      return summaryProfileList
    }
  
    private fun convertToProfileMetrics(
      profile: ProfileType,
      metricList: MutableList&lt;Map&lt;String, Serializable&gt;&gt;
    ): ProfileMetrics {
      return if (profile == ProfileType.field)
        ProfileMetrics(fieldProfileMetrics = metricList)
      else
        ProfileMetrics(summaryProfileMetrics = metricList)
    }
  
    private fun convertToProfileMetrics(
      fieldMetricList: MutableList&lt;Map&lt;String, Serializable&gt;&gt;,
      summaryMetricList: MutableList&lt;Map&lt;String, Serializable&gt;&gt;
    ): ProfileMetrics {
      return ProfileMetrics(
        fieldProfileMetrics = fieldMetricList,
        summaryProfileMetrics = summaryMetricList
      )
    }
  
  
    companion object {
      val logger = LoggerFactory.getLogger(ProfileMetricsService::class.java)
    }
  }
  </code></pre>
  <p>The interface generated after generate-sources gives this</p>
  <pre><code>interface ProfileMetricsApiService {
  
      /**
       * GET /v1/metrics : Get list of field and summary profile metrics
       * Retrieves list of metrics for field and summary profile
       *
       * @param profile Profile Type (optional)
       * @return OK (status code 200)
       *         or Bad Request. (status code 400)
       *         or The specified resource was not found. (status code 404)
       *         or Unexpected error. (status code 200)
       * @see ProfileMetricsApi#v1MetricsGet
       */
      fun v1MetricsGet(profile: ProfileType?): ProfileMetrics
  }
  </code></pre>
  <p>ProfileMetrics after compilation generates this</p>
  <pre><code>/**
   * List of metrics available in Field Profile
   * @param fieldProfileMetrics 
   * @param summaryProfileMetrics 
   */
  data class ProfileMetrics(
  
      @field:Valid
      @Schema(example = &quot;null&quot;, description = &quot;&quot;)
      @field:JsonProperty(&quot;fieldProfileMetrics&quot;) val fieldProfileMetrics: kotlin.collections.List&lt;kotlin.Any&gt;? = null,
  
      @field:Valid
      @Schema(example = &quot;null&quot;, description = &quot;&quot;)
      @field:JsonProperty(&quot;summaryProfileMetrics&quot;) val summaryProfileMetrics: kotlin.collections.List&lt;kotlin.Any&gt;? = null
  ) {
  
  }
  </code></pre>
  <p>This is how the response looks like <a href="https://i.stack.imgur.com/1FyDg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1FyDg.png" alt="enter image description here" /></a></p>
  <p>Any help is appreciated.</p>