Skip to content

Commit

Permalink
fix: Openapi-generator array schema with nested array (#4630)
Browse files Browse the repository at this point in the history
* fix: generate nested array objects

* added changelog

* update test services

* Update .changeset/heavy-terms-dance.md

Co-authored-by: Deeksha Sinha <88374536+deekshas8@users.noreply.github.com>

* separate impl for simple and nested arrays

* added comment

* refactoring

* Changes from lint:fix

* small fix

* modify test for nested array

* Changes from lint:fix

* changes to tests

---------

Co-authored-by: Deeksha Sinha <88374536+deekshas8@users.noreply.github.com>
Co-authored-by: cloud-sdk-js <cloud-sdk-js@github.com>
  • Loading branch information
3 people committed Apr 16, 2024
1 parent 66be02a commit ff3ede6
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-terms-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sap-cloud-sdk/openapi-generator": minor
---

[Fixed Issue] Fix nested array type in generated OpenAPI schemas such that array wraps the whole object.
44 changes: 44 additions & 0 deletions packages/openapi-generator/src/file-serializer/schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,53 @@ describe('serializeSchema for object schemas', () => {
} & Record<string, string>"
`);
});

it('serializes object schema with array items and no additional properties', () => {
expect(
serializeSchema({
properties: [
{
name: 'objectArrayProp',
required: true,
schema: {
uniqueItems: false,
items: {
properties: [
{
name: 'name',
required: true,
schema: { type: 'string' },
schemaProperties: {}
}
],
additionalProperties: { type: 'any' }
}
},
schemaProperties: {}
}
],
additionalProperties: { type: 'any' }
})
).toMatchInlineSnapshot(`
"{
'objectArrayProp': ({
'name': string;
} & Record<string, any>)[];
} & Record<string, any>"
`);
});
});

describe('serializeSchema for array schemas', () => {
it('serializes array schema', () => {
expect(
serializeSchema({
uniqueItems: false,
items: { type: 'string' }
})
).toEqual('string[]');
});

it('serializes array schema with nested array', () => {
expect(
serializeSchema({
Expand Down
8 changes: 7 additions & 1 deletion packages/openapi-generator/src/file-serializer/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ export function serializeSchema(schema: OpenApiSchema): string {
if (isReferenceObject(schema)) {
return schema.schemaName;
}

if (isArraySchema(schema)) {
const type = serializeSchema(schema.items);
return schema.uniqueItems ? `Set<${type}>` : `${type}[]`;
return schema.uniqueItems
? `Set<${type}>`
: 'properties' in schema.items
? `(${type})[]`
: `${type}[]`;
}

if (isObjectSchema(schema)) {
return serializeObjectSchema(schema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import type { TestEntity } from './test-entity';
*/
export type ComplexTestEntity = {
referenceProperty?: SimpleTestEntity;
arrayProperty?: {
arrayProperty?: ({
item?: string;
} & Record<string, any>[];
} & Record<string, any>)[];
uniqueItemsProperty?: Set<string>;
requiredPropertiesProperty?: {
optionalProperty?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import type { TestEntity } from './test-entity';
*/
export type ComplexTestEntity = {
referenceProperty?: SimpleTestEntity;
arrayProperty?: {
arrayProperty?: ({
item?: string;
} & Record<string, any>[];
} & Record<string, any>)[];
uniqueItemsProperty?: Set<string>;
requiredPropertiesProperty?: {
optionalProperty?: string;
Expand Down

0 comments on commit ff3ede6

Please sign in to comment.