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

prefixItems is unsupported (OpenAPI 3.1) #890

Closed
syniex opened this issue Jul 8, 2023 · 7 comments · Fixed by #1175
Closed

prefixItems is unsupported (OpenAPI 3.1) #890

syniex opened this issue Jul 8, 2023 · 7 comments · Fixed by #1175
Assignees
Labels
openapi_31 OpenAPI 3.1 related issue
Milestone

Comments

@syniex
Copy link

syniex commented Jul 8, 2023

What are the steps to reproduce this issue?

  1. use prefixItems in the openapi (version 3.1)

What happens?

Orval will crash saying ovt - Error: All arrays must have an items key define

What were you expecting to happen?

Client will be generated

Any logs, error output, etc?

Swagger Example
image

Any other comments?

{
    "openapi": "3.1.0",
    "info": {
        "title": "Fail Example",
        "version": "0.1.0"
    },
    "paths": {
        "/api/v1/building/": {
            "get": {
                "tags": [
                    "Version 1",
                    "Building"
                ],
                "summary": "Get Buildings",
                "operationId": "get_buildings",
                "responses": {
                    "200": {
                        "description": "Successful Response",
                        "content": {
                            "application/json": {
                                "schema": {
                                    "$ref": "#/components/schemas/Polygon"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "Polygon": {
                "properties": {
                    "type": {
                        "const": "Polygon",
                        "title": "Type",
                        "default": "Polygon"
                    },
                    "coordinates": {
                        "items": {
                            "items": {
                                "prefixItems": [
                                    {
                                        "type": "number"
                                    },
                                    {
                                        "type": "number"
                                    }
                                ],
                                "type": "array",
                                "maxItems": 2,
                                "minItems": 2
                            },
                            "type": "array"
                        },
                        "type": "array",
                        "title": "Coordinates"
                    }
                },
                "type": "object",
                "required": [
                    "coordinates"
                ],
                "title": "Polygon",
                "description": "Polygon Model."
            }
        }
    }
}
@dan-garden
Copy link

Having the same issue, everywhere else my spec is valid. Can't find any info online for this specific issue either.

@GuillaumeQuenneville
Copy link

GuillaumeQuenneville commented Jul 31, 2023

I can confirm this issue. Using python and pydantic, defining a type with a tuple_positional_schema generates a prefixItems key. Changing the type's json schema to a List instead of a Tuple works as a temporary (less robust ) fix.

@guy032
Copy link

guy032 commented Aug 13, 2023

I narrowed this error to a change in my swagger.json from this (works):

{
	"description": "List of connectors",
	"content": {
		"application/json": {
			"schema": {
				"$ref": "#/components/schemas/ConnectorDto"
			}
		}
	}
}

to this (raise the Error: All arrays must have an items key define):

{
	"description": "List of connectors",
	"content": {
		"application/json": {
			"schema": {
				"allOf": [{
					"$ref": "#/components/schemas/PageDto"
				}, {
					"properties": {
						"results": {
							"type": "array",
							"items": {
								"$ref": "#/components/schemas/ConnectorDto"
							}
						}
					}
				}]
			}
		}
	}
}

And this is the code that generates this schema for me:

import type { Type } from '@nestjs/common';
import { applyDecorators } from '@nestjs/common';
import { ApiExtraModels, ApiOkResponse, getSchemaPath } from '@nestjs/swagger';

import { PageDto } from '../common/dto/page.dto';

export function ApiPageOkResponse<T extends Type>(options: {
  type: T;
  description?: string;
}): MethodDecorator {
  return applyDecorators(
    ApiExtraModels(PageDto),
    ApiExtraModels(options.type),
    ApiOkResponse({
      description: options.description,
      schema: {
        allOf: [
          { $ref: getSchemaPath(PageDto) },
          {
            properties: {
              results: {
                type: 'array',
                items: { $ref: getSchemaPath(options.type) },
              },
            },
          },
        ],
      },
    }),
  );
}

@rauldeheer
Copy link

Any news on this?

@Pierstoval
Copy link

Pierstoval commented Sep 13, 2023

Just encountered the same error, as well as countless must have required property '$ref' or must match exactly one schema in oneOf errors alongside the Error: All arrays must have an ``items`` key define error 😕

Is there any workaround so far?

@rauldeheer
Copy link

@Pierstoval I was able to fix it. My Swagger doc is auto-generated by NestJS Swagger. The problem was that arrays of a 'simple type' such as a number or a string didn't have a type in the Swagger JSON. So at first I had:

@ApiProperty({ required: false, isArray: true })
@Expose()
numberArray?: number[];

Once I changed all those arrays to the following:

@ApiProperty({ required: false, isArray: true, type: Number })
@Expose()
numberArray?: number[];

The schema added the correct type and it was instantly fixed. So all I had to do was add the correct type property to the ApiProperty decorator. Hope this helps!

@melloware melloware added enhancement New feature or request openapi_31 OpenAPI 3.1 related issue and removed enhancement New feature or request labels Nov 10, 2023
@jrozbicki
Copy link

I get the same error, would be very helpful if error was correlated with schema line number - that would help to narrow down the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
openapi_31 OpenAPI 3.1 related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants