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

feat: support for prefixItems #1175

Merged
merged 1 commit into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions packages/core/src/getters/array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SchemaObject } from 'openapi3-ts/oas30';
import { ReferenceObject, SchemaObject } from 'openapi3-ts/oas30';
import { SchemaObject as SchemaObject31 } from 'openapi3-ts/oas31';
import { ContextSpecs, ScalarValue } from '../types';
import { resolveObject } from '../resolvers/object';
Expand All @@ -18,14 +18,43 @@ export const getArray = ({
name?: string;
context: ContextSpecs;
}): ScalarValue => {
if (schema.items) {
const schema31 = schema as SchemaObject31;
if (schema31.prefixItems) {
// TODO: https://github.com/anymaniax/orval/issues/890
throw new Error(
`prefixItems is not supported (name=${name}, schema=${JSON.stringify(schema)})`,
);
const schema31 = schema as SchemaObject31;
if (schema31.prefixItems) {
const resolvedObjects = schema31.prefixItems.map((item, index) =>
resolveObject({
schema: item as SchemaObject | ReferenceObject,
propName:
name + context.output.override.components.schemas.itemSuffix + index,
context,
}),
);
if (schema31.items) {
const additional = resolveObject({
schema: schema31.items as SchemaObject | ReferenceObject,
propName:
name +
context.output.override.components.schemas.itemSuffix +
'Additional',
context,
});
resolvedObjects.push({
...additional,
value: `...${additional.value}[]`,
});
}
return {
type: 'array',
isEnum: false,
isRef: false,
value: `[${resolvedObjects.map((o) => o.value).join(', ')}]`,
imports: resolvedObjects.flatMap((o) => o.imports),
schemas: resolvedObjects.flatMap((o) => o.schemas),
hasReadonlyProps: resolvedObjects.some((o) => o.hasReadonlyProps),
example: schema.example,
examples: resolveExampleRefs(schema.examples, context),
};
}
if (schema.items) {
const resolvedObject = resolveObject({
schema: schema.items,
propName: name + context.output.override.components.schemas.itemSuffix,
Expand Down
8 changes: 8 additions & 0 deletions tests/configs/default.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,12 @@ export default defineConfig({
target: '../generated/default/deeply-nested-refs/endpoints.ts',
},
},
'example-v3-1': {
input: '../specifications/example-v3-1.yaml',
output: {
mock: true,
schemas: '../generated/default/example-v3-1/model',
target: '../generated/default/example-v3-1/endpoints.ts',
},
},
});
30 changes: 30 additions & 0 deletions tests/specifications/example-v3-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
openapi: 3.1.0
info:
title: Nullables
description: 'OpenAPI 3.1 examples'
version: 1.0.0
paths: {}
components:
schemas:
Test:
properties:
example_tuple:
maxItems: 2
minItems: 2
prefixItems:
- type: string
- {}
type: array
title: Example tuple
example_tuple_additional:
maxItems: 2
minItems: 2
prefixItems:
- type: string
- {}
items:
type: string
type: array
title: Example tuple
title: Test
type: object