swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

Inconsistent behaviour for oneOf

Open andmatei opened this issue 3 years ago • 1 comments

When specifying in a @Schema the field oneOf alongside discriminatorMapping, in resulting OpenAPI specification all the properties of the parent are also copied in the children specified in oneOf.

Code example

@Schema(
    discriminatorProperty = "discriminator",
    discriminatorMapping = {
      @DiscriminatorMapping(
          value ="A",
          schema = A.class),
      @DiscriminatorMapping(
          value = "B",
          schema = B.class)
    },
    oneOf = {
      A.class,
      B.class,
    })
public abstract class Parent {
  
    @Schema(name = "discriminator")
    public String discriminator;
  
   @Schema(name = "test")
    public int test;
}

@Schema
public class A extends Parent {
    @Schema(name = "testA")
    public int testA;
}

@Schema
public class B extends Parent {
    @Schema(name = "testB")
    public int testB;
}

Resulting OpenAPI spec:

Parent:
   type: object
   oneOf:
   - $ref: '#/components/schemas/A'
   - $ref: '#/components/schemas/B'
   discriminator:
      mapping:
         A: '#/components/schemas/A'
         B: '#/components/schemas/B'
        propertyName: providerName
   properties:
      discriminator:
         type: string
      test:
         type: string
A:
   type: object
   allOf:
   - $ref: '#/components/schemas/Parent'
   - type: object
     properties:
        testA:
           type: string
B:
   type: object
   allOf:
   - $ref: '#/components/schemas/Parent'
   - type: object
     properties:
        testB:
           type: string

But when specifying in a @Schema the field oneOf without discriminatorMapping, the children specified in the oneOf have an allOf property with a references to the parent.

Code example (same as above, but without the discriminator)

@Schema(
    oneOf = {
      A.class,
      B.class,
    })
public abstract class Parent {
  
    @Schema(name = "discriminator")
    public String discriminator;
  
   @Schema(name = "test")
    public String test;
}

@Schema
public class A extends Parent {
    @Schema(name = "testA")
    public String testA;
}

@Schema
public class B extends Parent {
    @Schema(name = "testB")
    public String testB;
}

Resulting OpenAPI spec:

Parent:
   type: object
   properties:
      discriminator:
         type: string
      test:
         type: string
A:
   type: object
   properties:
      discriminator:
         type: string
      test:
         type: string
      testA:
         type: string
B:
   type: object
   properties:
      discriminator:
         type: string
      test:
         type: string
      testB:
         type: string

This seems a bit like an inconsistent behaviour. I was wondering if this was intended or if I am missing something?

andmatei avatar Jan 20 '23 21:01 andmatei

Related: https://github.com/swagger-api/swagger-core/issues/4156#issuecomment-2191325984

sh-at-cs avatar Jul 23 '25 14:07 sh-at-cs