Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValidateOneOfDiscriminator errors #383

Closed
michivogler opened this issue Feb 18, 2019 · 3 comments
Closed

ValidateOneOfDiscriminator errors #383

michivogler opened this issue Feb 18, 2019 · 3 comments
Labels
type:bug A broken experience
Milestone

Comments

@michivogler
Copy link

I'm currently trying to generate an OpenAPI documentation using this library. When I try to add a discriminator in combination with oneOf I get a ton of errors after validation for the ValidateOneOfDiscriminator rule and I can't figure out what exactly is wrong.

Error message examples:

Composite Schema EmailAddress must contain property specified in the discriminator Type. [#/paths/~1Addresses/get/responses/200/content/application~1json/schema/items/oneOf]

Composite schema EmailAddress must contain property specified in the discriminator Type in the required field list. [#/paths/~1Addresses/get/responses/200/content/application~1json/schema/items/oneOf]

When I serialize the OpenApiDocument and copy it to https://editor.swagger.io/ everything seems to be valid. Even if I use the OpenApiStreamReader and validate it again there are no errors and after serializing it again there's no diff between the two documents.

Any idea what the problem might be?

Here's a short version of my openapi.yaml

openapi: 3.0.1
info:
  title: My API
  version: v1
paths:
  /Addresses:
    get:
      tags:
        - Addresses
      operationId: AddressesGetAll
      parameters:
        - name: pageIndex
          in: query
          schema:
            type: string
        - name: pageSize
          in: query
          schema:
            type: string
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  oneOf:
                    - $ref: '#/components/schemas/EmailAddress'
                    - $ref: '#/components/schemas/PostalAddress'
                    - $ref: '#/components/schemas/WebAddress'
                  discriminator:
                    propertyName: Type
        '401':
          description: Unauthorized
        '403':
          description: Forbidden
components:
  schemas:
    WebAddress:
      required:
        - Type
        - Uri
      type: object
      properties:
        VerifiedDate:
          type: string
          format: date-time
          readOnly: true
        Version:
          type: number
          format: int32
          readOnly: true
        CreatedTime:
          type: string
          format: date-time
          readOnly: true
        UpdatedTime:
          type: string
          format: date-time
          readOnly: true
        IsVerified:
          type: boolean
          readOnly: true
        Type:
          type: string
          readOnly: true
        VerificationCode:
          type: string
        Guid:
          type: string
          format: uuid
          readOnly: true
        IsAdvertisingAllowed:
          type: boolean
        Uri:
          type: string
        DisplayName:
          type: string
          readOnly: true
    EmailAddress:
      required:
        - Address
        - Type
      type: object
      properties:
        Address:
          type: string
        VerifiedDate:
          type: string
          format: date-time
          readOnly: true
        Version:
          type: number
          format: int32
          readOnly: true
        CreatedTime:
          type: string
          format: date-time
          readOnly: true
        UpdatedTime:
          type: string
          format: date-time
          readOnly: true
        IsVerified:
          type: boolean
          readOnly: true
        Domain:
          type: string
          readOnly: true
        Type:
          type: string
          readOnly: true
        VerificationCode:
          type: string
        Guid:
          type: string
          format: uuid
          readOnly: true
        IsAdvertisingAllowed:
          type: boolean
        DisplayName:
          type: string
          readOnly: true
    PostalAddress:
      required:
        - Country
        - City
        - Type
      type: object
      properties:
        Longitude:
          type: string
        Region:
          type: string
        Zipcode:
          type: string
        City:
          type: string
        VerifiedDate:
          type: string
          format: date-time
          readOnly: true
        Version:
          type: number
          format: int32
          readOnly: true
        CreatedTime:
          type: string
          format: date-time
          readOnly: true
        State:
          type: string
        UpdatedTime:
          type: string
          format: date-time
          readOnly: true
        StreetName:
          type: string
        IsVerified:
          type: boolean
          readOnly: true
        Type:
          type: string
          readOnly: true
        VerificationCode:
          type: string
        Guid:
          type: string
          format: uuid
          readOnly: true
        Latitude:
          type: string
        IsAdvertisingAllowed:
          type: boolean
        DisplayName:
          type: string
          readOnly: true
        StreetNumber:
          type: string
        PoBox:
          type: string
@michivogler
Copy link
Author

Here's a test to reproduce the error:

[Fact]
public void ValidateOneOfDiscriminatorTest()
{
	var openApiDocument = new OpenApiDocument
	{
		Info = new OpenApiInfo {Title = "My API", Version = "v1"},
		Components = new OpenApiComponents(),
		Paths = new OpenApiPaths
		{
			["/people"] = new OpenApiPathItem
			{
				Operations = new Dictionary<OperationType, OpenApiOperation>
				{
					[OperationType.Get] = new OpenApiOperation
					{
						Description = "Returns all people",
						Responses = new OpenApiResponses
						{
							["200"] = new OpenApiResponse
							{
								Description = "OK",
								Content = new Dictionary<string, OpenApiMediaType>
								{
									["application/json"] = new OpenApiMediaType
									{
										Schema = new OpenApiSchema
										{
											Type = "array",
											Items = new OpenApiSchema
											{
												Discriminator = new OpenApiDiscriminator
												{
													PropertyName = "Type"
												},
												OneOf = new List<OpenApiSchema>
												{
													new OpenApiSchema
													{
														Reference = new OpenApiReference
														{
															Type = ReferenceType.Schema,
															Id = "Person"
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	};

	openApiDocument.Components.Schemas.Add("Person", new OpenApiSchema
	{
		Type = "object",
		Required = new HashSet<string> {"Type", "Name"},
		Properties = new Dictionary<string, OpenApiSchema>
		{
			{"Type", new OpenApiSchema {Type = "string"}},
			{ "Name", new OpenApiSchema {Type = "string"}}
		}
	});

	var openApiErrors = openApiDocument.Validate(ValidationRuleSet.GetDefaultRuleSet());
	Assert.Empty(openApiErrors);
}

@PerthCharern
Copy link
Contributor

#384 should partially fix this, but I think there's still the same bug with #402 that remains. We are not looking into the properties of the children schema to check the required properties.

@PerthCharern PerthCharern added the type:bug A broken experience label Apr 28, 2019
@darrelmiller darrelmiller added this to the Backlog milestone Apr 26, 2020
@baywet
Copy link
Member

baywet commented Jun 28, 2022

closing as duplicate of #402 as we have more people engaged on 402

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug A broken experience
Projects
None yet
Development

No branches or pull requests

4 participants