Skip to content

Commit

Permalink
Arrays without an items schema
Browse files Browse the repository at this point in the history
They can now use the new ANY type
  • Loading branch information
karlvr committed Jul 29, 2024
1 parent 8f4555b commit 9005afb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-keys-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openapi-generator-plus/core": minor
---

Add support for arrays without a specified items schema
15 changes: 14 additions & 1 deletion packages/core/src/__tests__/openapiv31.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createTestDocument } from './common'
import { idx } from '..'
import { CodegenAllOfStrategy, CodegenObjectSchema, CodegenSchemaType } from '@openapi-generator-plus/types'
import { CodegenAllOfStrategy, CodegenArraySchema, CodegenObjectSchema, CodegenSchemaType } from '@openapi-generator-plus/types'
import { findProperty } from '../process/schema/utils'

test('process document', async() => {
Expand Down Expand Up @@ -98,3 +98,16 @@ test('any schema', async() => {
const property = idx.get(schema.properties!, 'test')
expect(property?.schema.schemaType).toBe(CodegenSchemaType.ANY)
})

test('array without items schema', async() => {
const result = await createTestDocument('openapiv31/array-without-items-schema.yml')
expect(result).toBeDefined()

const schema = idx.get(result.schemas, 'TestObject') as CodegenObjectSchema
expect(schema.schemaType).toBe(CodegenSchemaType.OBJECT)

const property = idx.get(schema.properties!, 'test')
expect(property?.schema.schemaType).toBe(CodegenSchemaType.ARRAY)

expect((property?.schema as CodegenArraySchema).component.schema.schemaType).toBe(CodegenSchemaType.ANY)
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: 3.1.0
info:
title: Array without items schema
description: Lorem ipsum
version: '1.0.1'
servers:
- url: http://example.com/api/v1
description: Example server
- url: https://example.com/api/v1
paths: {}
components:
schemas:
TestObject:
type: object
properties:
test:
type: array
6 changes: 1 addition & 5 deletions packages/core/src/process/schema/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,10 @@ export function toCodegenArraySchema(apiSchema: OpenAPIX.SchemaObject, naming: S
throw new Error('Not an array schema')
}

if (!apiSchema.items) {
throw new Error('items missing for schema type "array"')
}

const vendorExtensions = toCodegenVendorExtensions(apiSchema)

/* Component properties are implicitly required as we don't expect to have `null` entries in the array. */
const componentSchemaUsage = toCodegenSchemaUsage(apiSchema.items, state, {
const componentSchemaUsage = toCodegenSchemaUsage(apiSchema.items || {}, state, {
required: true,
suggestedName: suggestedItemModelName,
purpose: CodegenSchemaPurpose.ARRAY_ITEM,
Expand Down

0 comments on commit 9005afb

Please sign in to comment.