openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG][JAVA][SPRING] Validating of the REST API call fails when a request with required discriminator should be sent

Open LeosBitto opened this issue 6 days ago • 0 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

We use openapi-generator-maven-plugin to generate interfaces for the REST clients, which we then create using RestClient and HttpServiceProxyFactory. When a request with a required discriminator should be sent the validation of this request (by ResponseValidatingMethodInterceptor) fails, because the required discriminator value is not filled in yet (it would be done later by Jackson).

openapi-generator version

7.27.0

OpenAPI declaration file content or url
openapi: 3.0.3
info:
  title: Validation Probe API
  description: >-
    Neutral test-only contract exercising the shared validation of generated HTTP interface clients.
  version: 1.0.0
paths:
  /validation-probe/request/discriminator:
    post:
      tags: [ValidationProbe]
      operationId: writeDiscriminatedRequest
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/DiscriminatedRequest'
      responses:
        '200':
          description: Payload accepted
components:
  schemas:
    DiscriminatedRequest:
      type: object
      discriminator:
        propertyName: type
        mapping:
          text: '#/components/schemas/DiscriminatedTextRequest'
      required: [type, value]
      properties:
        type:
          type: string
        value:
          type: string
    DiscriminatedTextRequest:
      allOf:
        - $ref: '#/components/schemas/DiscriminatedRequest'
Generation Details

Use openapi-generator-maven-plugin with generatorName=spring useBeanValidation=true

Steps to reproduce

Use the following code to create a REST API client using the generated interface as the target:

    public static <T> T createRestClientProxy(Class<T> target, RestClient restClient) {
        var adapter = RestClientAdapter.create(restClient);
        return HttpServiceProxyFactory.builderFor(adapter).build().createClient(target);
    }

Then call restClientProxy.writeDiscriminatedRequest(new DiscriminatedTextRequest().value("valid"));

Related issues/PRs

not found

Suggest a fix

Do not generate @NotNull for discriminator when Jackson is supposed to fill in the discriminator value.

LeosBitto avatar Nov 26 '26 15:11 LeosBitto