From e596773200e5a549dfdaba9d270b6f6b53ce1135 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Thu, 29 Aug 2024 17:51:57 +0200 Subject: [PATCH 01/22] add schema prefix option --- packages/openapi-generator/src/generator.ts | 2 +- .../src/options/generator-options.spec.ts | 3 ++- .../openapi-generator/src/options/options.ts | 11 ++++++++++ .../openapi-generator/src/parser/options.ts | 4 ++++ .../openapi-generator/src/parser/refs.spec.ts | 20 +++++++++++++++++++ packages/openapi-generator/src/parser/refs.ts | 2 +- 6 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/openapi-generator/src/generator.ts b/packages/openapi-generator/src/generator.ts index 5d63405120..733f6632cb 100644 --- a/packages/openapi-generator/src/generator.ts +++ b/packages/openapi-generator/src/generator.ts @@ -264,7 +264,7 @@ async function generateService( const parsedOpenApiDocument = await parseOpenApiDocument( openApiDocument, serviceOptions, - { strictNaming: !options.skipValidation } + { strictNaming: !options.skipValidation, schemaPrefix: options.schemaPrefix } ); const serviceDir = resolve(options.outputDir, serviceOptions.directoryName); diff --git a/packages/openapi-generator/src/options/generator-options.spec.ts b/packages/openapi-generator/src/options/generator-options.spec.ts index 0a36265772..dffea06587 100644 --- a/packages/openapi-generator/src/options/generator-options.spec.ts +++ b/packages/openapi-generator/src/options/generator-options.spec.ts @@ -37,7 +37,8 @@ describe('parseGeneratorOptions', () => { verbose: false, overwrite: false, config: undefined, - generateESM: false + generateESM: false, + schemaPrefix: undefined }; it('gets default options', () => { diff --git a/packages/openapi-generator/src/options/options.ts b/packages/openapi-generator/src/options/options.ts index 296890255a..51374cbf37 100644 --- a/packages/openapi-generator/src/options/options.ts +++ b/packages/openapi-generator/src/options/options.ts @@ -15,6 +15,11 @@ export type GeneratorOptions = CommonGeneratorOptions & OpenAPIGeneratorOptions; * Options to configure OpenAPI client generation when using the generator programmatically. */ export interface OpenAPIGeneratorOptions { + /** + * Prefix all schema names with a value. + * @experimental + */ + schemaPrefix?: string; /** * Whether to generate ECMAScript modules instead of CommonJS modules. */ @@ -37,5 +42,11 @@ export const cliOptions = { 'When enabled, all generated files follow the ECMAScript module syntax.', type: 'boolean', default: false + }, + schemaPrefix: { + describe: + 'When enabled, all generated files follow the ECMAScript module syntax.', + type: 'string', + default: undefined } } as const satisfies Options; diff --git a/packages/openapi-generator/src/parser/options.ts b/packages/openapi-generator/src/parser/options.ts index ba000eebe4..167850f594 100644 --- a/packages/openapi-generator/src/parser/options.ts +++ b/packages/openapi-generator/src/parser/options.ts @@ -7,4 +7,8 @@ export interface ParserOptions { * Fail parsing on conflicting names. */ strictNaming: boolean; + /** + * Add prefix to schema names + */ + schemaPrefix?: string; } diff --git a/packages/openapi-generator/src/parser/refs.spec.ts b/packages/openapi-generator/src/parser/refs.spec.ts index a4f4d0c7a0..2c4f192321 100644 --- a/packages/openapi-generator/src/parser/refs.spec.ts +++ b/packages/openapi-generator/src/parser/refs.spec.ts @@ -60,6 +60,26 @@ describe('OpenApiDocumentRefs', () => { ); }); + + it('gets the schema naming for reference object with a prefix', async () => { + refs = await createRefs( + { + ...emptyDocument, + components: { schemas: { typeName } } + }, + { strictNaming: true, schemaPrefix: 'xyz' } + ); + + expect( + refs.getSchemaNaming({ + $ref: '#/components/schemas/typeName' + }) + ).toEqual({ + schemaName: 'XyzTypeName', + fileName: 'type-name' + }); + }); + it('renames a schema if needed due to illegal names', async () => { refs = await createRefs( { diff --git a/packages/openapi-generator/src/parser/refs.ts b/packages/openapi-generator/src/parser/refs.ts index 188f4d9fa1..717111ab9a 100644 --- a/packages/openapi-generator/src/parser/refs.ts +++ b/packages/openapi-generator/src/parser/refs.ts @@ -72,7 +72,7 @@ export class OpenApiDocumentRefs { (mapping, originalName, i) => ({ ...mapping, [`#/components/schemas/${originalName}`]: { - schemaName: schemaNames[i], + schemaName: pascalCase(options.schemaPrefix ?? '') + schemaNames[i], fileName: fileNames[i] } }), From b60c5e0c2dfb9b20e4cef3cc679edc2859e32799 Mon Sep 17 00:00:00 2001 From: cloud-sdk-js Date: Fri, 30 Aug 2024 07:57:18 +0000 Subject: [PATCH 02/22] Changes from lint:fix --- packages/openapi-generator/src/generator.ts | 5 ++++- packages/openapi-generator/src/options/options.ts | 2 +- packages/openapi-generator/src/parser/options.ts | 2 +- packages/openapi-generator/src/parser/refs.spec.ts | 1 - 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/openapi-generator/src/generator.ts b/packages/openapi-generator/src/generator.ts index 733f6632cb..f0c8c509c0 100644 --- a/packages/openapi-generator/src/generator.ts +++ b/packages/openapi-generator/src/generator.ts @@ -264,7 +264,10 @@ async function generateService( const parsedOpenApiDocument = await parseOpenApiDocument( openApiDocument, serviceOptions, - { strictNaming: !options.skipValidation, schemaPrefix: options.schemaPrefix } + { + strictNaming: !options.skipValidation, + schemaPrefix: options.schemaPrefix + } ); const serviceDir = resolve(options.outputDir, serviceOptions.directoryName); diff --git a/packages/openapi-generator/src/options/options.ts b/packages/openapi-generator/src/options/options.ts index 51374cbf37..d0c02ecbca 100644 --- a/packages/openapi-generator/src/options/options.ts +++ b/packages/openapi-generator/src/options/options.ts @@ -45,7 +45,7 @@ export const cliOptions = { }, schemaPrefix: { describe: - 'When enabled, all generated files follow the ECMAScript module syntax.', + 'When enabled, all generated files follow the ECMAScript module syntax.', type: 'string', default: undefined } diff --git a/packages/openapi-generator/src/parser/options.ts b/packages/openapi-generator/src/parser/options.ts index 167850f594..3c4dbc8187 100644 --- a/packages/openapi-generator/src/parser/options.ts +++ b/packages/openapi-generator/src/parser/options.ts @@ -8,7 +8,7 @@ export interface ParserOptions { */ strictNaming: boolean; /** - * Add prefix to schema names + * Add prefix to schema names. */ schemaPrefix?: string; } diff --git a/packages/openapi-generator/src/parser/refs.spec.ts b/packages/openapi-generator/src/parser/refs.spec.ts index 2c4f192321..a400d961a5 100644 --- a/packages/openapi-generator/src/parser/refs.spec.ts +++ b/packages/openapi-generator/src/parser/refs.spec.ts @@ -60,7 +60,6 @@ describe('OpenApiDocumentRefs', () => { ); }); - it('gets the schema naming for reference object with a prefix', async () => { refs = await createRefs( { From 365dc09b3fd45281ddca1d0c7e42c5c4688854fb Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Fri, 30 Aug 2024 13:33:00 +0200 Subject: [PATCH 03/22] parse properties + required --- .../src/file-serializer/schema.ts | 7 ++++++- packages/openapi-generator/src/parser/schema.ts | 8 ++++++-- .../swagger-yaml-service/schema/index.d.ts | 2 ++ .../swagger-yaml-service/schema/index.js | 2 ++ .../swagger-yaml-service/schema/index.js.map | 2 +- .../swagger-yaml-service/schema/index.ts | 2 ++ .../schema/test-entity-2.d.ts | 12 ++++++++++++ .../schema/test-entity-2.js | 3 +++ .../schema/test-entity-2.js.map | 1 + .../schema/test-entity-2.ts | 12 ++++++++++++ .../schema/test-entity-3.d.ts | 12 ++++++++++++ .../schema/test-entity-3.js | 3 +++ .../schema/test-entity-3.js.map | 1 + .../schema/test-entity-3.ts | 12 ++++++++++++ .../swagger-yaml-service.yml | 17 +++++++++++++++++ 15 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.js create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.js.map create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.d.ts create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.js create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.js.map create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.ts diff --git a/packages/openapi-generator/src/file-serializer/schema.ts b/packages/openapi-generator/src/file-serializer/schema.ts index 45a8184b11..af739ee417 100644 --- a/packages/openapi-generator/src/file-serializer/schema.ts +++ b/packages/openapi-generator/src/file-serializer/schema.ts @@ -38,7 +38,12 @@ export function serializeSchema(schema: OpenApiSchema): string { } if (isObjectSchema(schema)) { - return serializeObjectSchema(schema); + let types: string[] = []; + if (isAllOfSchema(schema)) { + types.push(schema.allOf.map(type => serializeSchema(type)).join(' & ')); + } + types.push(serializeObjectSchema(schema)); + return types.join(' & '); } if (isEnumSchema(schema)) { diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index 8498e1003b..07eb7aa4ed 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -47,7 +47,10 @@ export function parseSchema( schema.properties || 'additionalProperties' in schema ) { - return parseObjectSchema(schema, refs, options); + return { + ...(schema.allOf?.length && parseXOfSchema(schema, refs, 'allOf', options)) , + ...((schema.properties || 'additionalProperties' in schema) && parseObjectSchema(schema, refs, options)) + } } if (schema.enum?.length) { @@ -221,8 +224,9 @@ function parseXOfSchema( xOf: 'oneOf' | 'allOf' | 'anyOf', options: ParserOptions ): any { + const required = schema.required; return { - [xOf]: (schema[xOf] || []).map(entry => parseSchema(entry, refs, options)) + [xOf]: (schema[xOf] || []).map(entry => parseSchema({...entry, required }, refs, options)) }; } diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.d.ts index b9d07f4e60..b736051202 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.d.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.d.ts @@ -4,3 +4,5 @@ * This is a generated file powered by the SAP Cloud SDK for JavaScript. */ export * from './test-entity'; +export * from './test-entity-2'; +export * from './test-entity-3'; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js index e7046a55d1..44ca42c316 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js @@ -20,4 +20,6 @@ Object.defineProperty(exports, "__esModule", { value: true }); * This is a generated file powered by the SAP Cloud SDK for JavaScript. */ __exportStar(require("./test-entity"), exports); +__exportStar(require("./test-entity-2"), exports); +__exportStar(require("./test-entity-3"), exports); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js.map b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js.map index f22c5b66d5..206023d27c 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js.map +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;GAIG;AACH,gDAA8B"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;GAIG;AACH,gDAA8B;AAC9B,kDAAgC;AAChC,kDAAgC"} \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.ts index b9d07f4e60..b736051202 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.ts @@ -4,3 +4,5 @@ * This is a generated file powered by the SAP Cloud SDK for JavaScript. */ export * from './test-entity'; +export * from './test-entity-2'; +export * from './test-entity-3'; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts new file mode 100644 index 0000000000..faa883a128 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved. + * + * This is a generated file powered by the SAP Cloud SDK for JavaScript. + */ +import type { TestEntity } from './test-entity'; +/** + * Representation of the 'TestEntity2' schema. + */ +export type TestEntity2 = TestEntity & { + booleanProperty: boolean; +} & Record; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.js b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.js new file mode 100644 index 0000000000..9f307b7ca3 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=test-entity-2.js.map \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.js.map b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.js.map new file mode 100644 index 0000000000..096bea642a --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.js.map @@ -0,0 +1 @@ +{"version":3,"file":"test-entity-2.js","sourceRoot":"","sources":["test-entity-2.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts new file mode 100644 index 0000000000..faa883a128 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved. + * + * This is a generated file powered by the SAP Cloud SDK for JavaScript. + */ +import type { TestEntity } from './test-entity'; +/** + * Representation of the 'TestEntity2' schema. + */ +export type TestEntity2 = TestEntity & { + booleanProperty: boolean; +} & Record; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.d.ts new file mode 100644 index 0000000000..6664ba2494 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.d.ts @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved. + * + * This is a generated file powered by the SAP Cloud SDK for JavaScript. + */ +import type { TestEntity } from './test-entity'; +/** + * Representation of the 'TestEntity3' schema. + */ +export type TestEntity3 = TestEntity & { + booleanProperty?: boolean; +} & Record; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.js b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.js new file mode 100644 index 0000000000..37c8edc410 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=test-entity-3.js.map \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.js.map b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.js.map new file mode 100644 index 0000000000..827ffdcd6e --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.js.map @@ -0,0 +1 @@ +{"version":3,"file":"test-entity-3.js","sourceRoot":"","sources":["test-entity-3.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.ts new file mode 100644 index 0000000000..6664ba2494 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.ts @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved. + * + * This is a generated file powered by the SAP Cloud SDK for JavaScript. + */ +import type { TestEntity } from './test-entity'; +/** + * Representation of the 'TestEntity3' schema. + */ +export type TestEntity3 = TestEntity & { + booleanProperty?: boolean; +} & Record; diff --git a/test-resources/openapi-service-specs/swagger-yaml-service.yml b/test-resources/openapi-service-specs/swagger-yaml-service.yml index 880bd9397b..7db5d726f1 100644 --- a/test-resources/openapi-service-specs/swagger-yaml-service.yml +++ b/test-resources/openapi-service-specs/swagger-yaml-service.yml @@ -54,3 +54,20 @@ definitions: type: integer description: An integer property required: false + TestEntity_2: + type: object + allOf: + - $ref: '#/definitions/TestEntity' + - properties: + booleanProperty: + type: boolean + required: + - booleanProperty + TestEntity_3: + type: object + allOf: + - $ref: '#/definitions/TestEntity' + properties: + booleanProperty: + type: boolean + From de00b16f445472a426935956de4332bb9d961784 Mon Sep 17 00:00:00 2001 From: cloud-sdk-js Date: Fri, 30 Aug 2024 11:35:23 +0000 Subject: [PATCH 04/22] Changes from lint:fix --- .../openapi-generator/src/file-serializer/schema.ts | 2 +- packages/openapi-generator/src/parser/schema.ts | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/openapi-generator/src/file-serializer/schema.ts b/packages/openapi-generator/src/file-serializer/schema.ts index af739ee417..2889b671f3 100644 --- a/packages/openapi-generator/src/file-serializer/schema.ts +++ b/packages/openapi-generator/src/file-serializer/schema.ts @@ -38,7 +38,7 @@ export function serializeSchema(schema: OpenApiSchema): string { } if (isObjectSchema(schema)) { - let types: string[] = []; + const types: string[] = []; if (isAllOfSchema(schema)) { types.push(schema.allOf.map(type => serializeSchema(type)).join(' & ')); } diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index 07eb7aa4ed..b48fd9b202 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -48,9 +48,11 @@ export function parseSchema( 'additionalProperties' in schema ) { return { - ...(schema.allOf?.length && parseXOfSchema(schema, refs, 'allOf', options)) , - ...((schema.properties || 'additionalProperties' in schema) && parseObjectSchema(schema, refs, options)) - } + ...(schema.allOf?.length && + parseXOfSchema(schema, refs, 'allOf', options)), + ...((schema.properties || 'additionalProperties' in schema) && + parseObjectSchema(schema, refs, options)) + }; } if (schema.enum?.length) { @@ -226,7 +228,9 @@ function parseXOfSchema( ): any { const required = schema.required; return { - [xOf]: (schema[xOf] || []).map(entry => parseSchema({...entry, required }, refs, options)) + [xOf]: (schema[xOf] || []).map(entry => + parseSchema({ ...entry, required }, refs, options) + ) }; } From 0fbc7a73ac2de13a926eacaea77bd91d92487e93 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Fri, 30 Aug 2024 14:14:29 +0200 Subject: [PATCH 05/22] fix test --- packages/openapi-generator/src/parser/schema.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index b48fd9b202..aa40c1e256 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -50,8 +50,7 @@ export function parseSchema( return { ...(schema.allOf?.length && parseXOfSchema(schema, refs, 'allOf', options)), - ...((schema.properties || 'additionalProperties' in schema) && - parseObjectSchema(schema, refs, options)) + ...parseObjectSchema(schema, refs, options) }; } From b863309f05af8fa361b4b59bd7300c68a81c25eb Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Fri, 30 Aug 2024 14:54:37 +0200 Subject: [PATCH 06/22] fix --- packages/openapi-generator/src/parser/schema.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index aa40c1e256..fbfdf8405d 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -1,6 +1,6 @@ import { createLogger } from '@sap-cloud-sdk/util'; import { OpenAPIV3 } from 'openapi-types'; -import { isReferenceObject } from '../schema-util'; +import { isAllOfSchema, isReferenceObject } from '../schema-util'; import { OpenApiArraySchema, OpenApiEnumSchema, @@ -47,11 +47,15 @@ export function parseSchema( schema.properties || 'additionalProperties' in schema ) { - return { - ...(schema.allOf?.length && - parseXOfSchema(schema, refs, 'allOf', options)), - ...parseObjectSchema(schema, refs, options) - }; + if (isAllOfSchema(schema)) { + return { + ...(schema.allOf?.length && + parseXOfSchema(schema, refs, 'allOf', options)), + ...((schema.properties || 'additionalProperties' in schema) && + parseObjectSchema(schema, refs, options)) + } + } + return parseObjectSchema(schema, refs, options); } if (schema.enum?.length) { From 8033f3ca89f31ce49e803a24d2220d972253ed3a Mon Sep 17 00:00:00 2001 From: cloud-sdk-js Date: Fri, 30 Aug 2024 12:56:52 +0000 Subject: [PATCH 07/22] Changes from lint:fix --- packages/openapi-generator/src/parser/schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index fbfdf8405d..6ccd328432 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -53,7 +53,7 @@ export function parseSchema( parseXOfSchema(schema, refs, 'allOf', options)), ...((schema.properties || 'additionalProperties' in schema) && parseObjectSchema(schema, refs, options)) - } + }; } return parseObjectSchema(schema, refs, options); } From 7a3eb24abf5ea228a70cba57c1bff75ca479e980 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Sat, 31 Aug 2024 19:30:24 +0200 Subject: [PATCH 08/22] array has type obj with allOf only --- packages/openapi-generator/src/file-serializer/schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-generator/src/file-serializer/schema.ts b/packages/openapi-generator/src/file-serializer/schema.ts index 2889b671f3..ee0cf34288 100644 --- a/packages/openapi-generator/src/file-serializer/schema.ts +++ b/packages/openapi-generator/src/file-serializer/schema.ts @@ -32,7 +32,7 @@ export function serializeSchema(schema: OpenApiSchema): string { const type = serializeSchema(schema.items); return schema.uniqueItems ? `Set<${type}>` - : 'properties' in schema.items + : ('properties' in schema.items || 'allOf' in schema.items) ? `(${type})[]` : `${type}[]`; } From 46c77b18f540d9ee97143c039260d2e8a0af3f96 Mon Sep 17 00:00:00 2001 From: cloud-sdk-js Date: Sat, 31 Aug 2024 17:32:56 +0000 Subject: [PATCH 09/22] Changes from lint:fix --- packages/openapi-generator/src/file-serializer/schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-generator/src/file-serializer/schema.ts b/packages/openapi-generator/src/file-serializer/schema.ts index ee0cf34288..495f6cd34b 100644 --- a/packages/openapi-generator/src/file-serializer/schema.ts +++ b/packages/openapi-generator/src/file-serializer/schema.ts @@ -32,7 +32,7 @@ export function serializeSchema(schema: OpenApiSchema): string { const type = serializeSchema(schema.items); return schema.uniqueItems ? `Set<${type}>` - : ('properties' in schema.items || 'allOf' in schema.items) + : 'properties' in schema.items || 'allOf' in schema.items ? `(${type})[]` : `${type}[]`; } From 1f52a1bda7ab8a875f8a11454ce21dfcf7adce0d Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Mon, 9 Sep 2024 14:38:43 +0200 Subject: [PATCH 10/22] fixes --- .../src/file-serializer/schema.ts | 9 ++---- .../openapi-generator/src/parser/schema.ts | 30 ++++++++----------- .../schema/test-entity-2.d.ts | 2 +- .../schema/test-entity-2.ts | 2 +- 4 files changed, 17 insertions(+), 26 deletions(-) diff --git a/packages/openapi-generator/src/file-serializer/schema.ts b/packages/openapi-generator/src/file-serializer/schema.ts index 495f6cd34b..492bd8358b 100644 --- a/packages/openapi-generator/src/file-serializer/schema.ts +++ b/packages/openapi-generator/src/file-serializer/schema.ts @@ -32,18 +32,13 @@ export function serializeSchema(schema: OpenApiSchema): string { const type = serializeSchema(schema.items); return schema.uniqueItems ? `Set<${type}>` - : 'properties' in schema.items || 'allOf' in schema.items + : ['properties', 'allOf', 'oneOf', 'anyOf'].some(prop => prop in schema.items) ? `(${type})[]` : `${type}[]`; } if (isObjectSchema(schema)) { - const types: string[] = []; - if (isAllOfSchema(schema)) { - types.push(schema.allOf.map(type => serializeSchema(type)).join(' & ')); - } - types.push(serializeObjectSchema(schema)); - return types.join(' & '); + return serializeObjectSchema(schema); } if (isEnumSchema(schema)) { diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index 6ccd328432..a647724f67 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -42,22 +42,6 @@ export function parseSchema( return parseArraySchema(schema, refs, options); } - if ( - schema.type === 'object' || - schema.properties || - 'additionalProperties' in schema - ) { - if (isAllOfSchema(schema)) { - return { - ...(schema.allOf?.length && - parseXOfSchema(schema, refs, 'allOf', options)), - ...((schema.properties || 'additionalProperties' in schema) && - parseObjectSchema(schema, refs, options)) - }; - } - return parseObjectSchema(schema, refs, options); - } - if (schema.enum?.length) { return parseEnumSchema(schema, options); } @@ -67,6 +51,10 @@ export function parseSchema( } if (schema.allOf?.length) { + if (schema.properties) { + schema.allOf.push({ properties: schema.properties }); + delete schema.properties; + } return parseXOfSchema(schema, refs, 'allOf', options); } @@ -74,6 +62,14 @@ export function parseSchema( return parseXOfSchema(schema, refs, 'anyOf', options); } + if ( + schema.type === 'object' || + schema.properties || + 'additionalProperties' in schema + ) { + return parseObjectSchema(schema, refs, options); + } + if (schema.not) { return { not: parseSchema(schema.not, refs, options) @@ -232,7 +228,7 @@ function parseXOfSchema( const required = schema.required; return { [xOf]: (schema[xOf] || []).map(entry => - parseSchema({ ...entry, required }, refs, options) + parseSchema(entry, refs, options) ) }; } diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts index faa883a128..915f3964fe 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts @@ -8,5 +8,5 @@ import type { TestEntity } from './test-entity'; * Representation of the 'TestEntity2' schema. */ export type TestEntity2 = TestEntity & { - booleanProperty: boolean; + booleanProperty?: boolean; } & Record; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts index faa883a128..915f3964fe 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts @@ -8,5 +8,5 @@ import type { TestEntity } from './test-entity'; * Representation of the 'TestEntity2' schema. */ export type TestEntity2 = TestEntity & { - booleanProperty: boolean; + booleanProperty?: boolean; } & Record; From 68569e233b4dd282a7a259b2c1a48fb941ca13a9 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Mon, 9 Sep 2024 14:49:02 +0200 Subject: [PATCH 11/22] lint fix + log --- .../openapi-generator/src/file-serializer/schema.ts | 4 +++- packages/openapi-generator/src/parser/schema.ts | 10 +++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/openapi-generator/src/file-serializer/schema.ts b/packages/openapi-generator/src/file-serializer/schema.ts index 492bd8358b..e3bcedf1a5 100644 --- a/packages/openapi-generator/src/file-serializer/schema.ts +++ b/packages/openapi-generator/src/file-serializer/schema.ts @@ -32,7 +32,9 @@ export function serializeSchema(schema: OpenApiSchema): string { const type = serializeSchema(schema.items); return schema.uniqueItems ? `Set<${type}>` - : ['properties', 'allOf', 'oneOf', 'anyOf'].some(prop => prop in schema.items) + : ['properties', 'allOf', 'oneOf', 'anyOf'].some( + prop => prop in schema.items + ) ? `(${type})[]` : `${type}[]`; } diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index a647724f67..7a5969e45d 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -1,6 +1,6 @@ import { createLogger } from '@sap-cloud-sdk/util'; import { OpenAPIV3 } from 'openapi-types'; -import { isAllOfSchema, isReferenceObject } from '../schema-util'; +import { isReferenceObject } from '../schema-util'; import { OpenApiArraySchema, OpenApiEnumSchema, @@ -52,6 +52,9 @@ export function parseSchema( if (schema.allOf?.length) { if (schema.properties) { + logger.info( + 'Detected schema with allOf and properties in the same level. This was refactored to a schema with allOf only, also containing all the properties from the top level.' + ); schema.allOf.push({ properties: schema.properties }); delete schema.properties; } @@ -225,11 +228,8 @@ function parseXOfSchema( xOf: 'oneOf' | 'allOf' | 'anyOf', options: ParserOptions ): any { - const required = schema.required; return { - [xOf]: (schema[xOf] || []).map(entry => - parseSchema(entry, refs, options) - ) + [xOf]: (schema[xOf] || []).map(entry => parseSchema(entry, refs, options)) }; } From f36935a02751d224e9169ad4ce091d395cce557b Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Thu, 12 Sep 2024 13:03:30 +0200 Subject: [PATCH 12/22] fix message --- packages/openapi-generator/src/parser/schema.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index 7a5969e45d..df6c92ffdf 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -53,7 +53,7 @@ export function parseSchema( if (schema.allOf?.length) { if (schema.properties) { logger.info( - 'Detected schema with allOf and properties in the same level. This was refactored to a schema with allOf only, also containing all the properties from the top level.' + 'Detected schema with allOf and properties in the same level. This was refactored to a schema with allOf only, containing all the properties from the top level.' ); schema.allOf.push({ properties: schema.properties }); delete schema.properties; From 491b0f80616f8f902cc2435e13e6115d93b87105 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Thu, 12 Sep 2024 15:59:03 +0200 Subject: [PATCH 13/22] empty string for prefix --- packages/openapi-generator/src/options/options.ts | 5 +++-- packages/openapi-generator/src/parser/options.ts | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/openapi-generator/src/options/options.ts b/packages/openapi-generator/src/options/options.ts index d0c02ecbca..8090acd019 100644 --- a/packages/openapi-generator/src/options/options.ts +++ b/packages/openapi-generator/src/options/options.ts @@ -45,8 +45,9 @@ export const cliOptions = { }, schemaPrefix: { describe: - 'When enabled, all generated files follow the ECMAScript module syntax.', + 'Prefix all schema names with a value. This is useful to avoid naming conflicts when multiple services are generated.', type: 'string', - default: undefined + default: '', + hidden: true } } as const satisfies Options; diff --git a/packages/openapi-generator/src/parser/options.ts b/packages/openapi-generator/src/parser/options.ts index 3c4dbc8187..51483b198a 100644 --- a/packages/openapi-generator/src/parser/options.ts +++ b/packages/openapi-generator/src/parser/options.ts @@ -10,5 +10,5 @@ export interface ParserOptions { /** * Add prefix to schema names. */ - schemaPrefix?: string; + schemaPrefix: string; } From aca3a1ce6a721b735893851d6785e3fc48ea7807 Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Thu, 12 Sep 2024 16:51:20 +0200 Subject: [PATCH 14/22] clean up --- packages/openapi-generator/src/generator.ts | 3 +-- .../src/options/generator-options.spec.ts | 3 +-- .../openapi-generator/src/options/options.ts | 12 ------------ .../openapi-generator/src/parser/options.ts | 4 ---- .../openapi-generator/src/parser/refs.spec.ts | 19 ------------------- packages/openapi-generator/src/parser/refs.ts | 2 +- 6 files changed, 3 insertions(+), 40 deletions(-) diff --git a/packages/openapi-generator/src/generator.ts b/packages/openapi-generator/src/generator.ts index f0c8c509c0..e6210887ff 100644 --- a/packages/openapi-generator/src/generator.ts +++ b/packages/openapi-generator/src/generator.ts @@ -265,8 +265,7 @@ async function generateService( openApiDocument, serviceOptions, { - strictNaming: !options.skipValidation, - schemaPrefix: options.schemaPrefix + strictNaming: !options.skipValidation } ); diff --git a/packages/openapi-generator/src/options/generator-options.spec.ts b/packages/openapi-generator/src/options/generator-options.spec.ts index dffea06587..0a36265772 100644 --- a/packages/openapi-generator/src/options/generator-options.spec.ts +++ b/packages/openapi-generator/src/options/generator-options.spec.ts @@ -37,8 +37,7 @@ describe('parseGeneratorOptions', () => { verbose: false, overwrite: false, config: undefined, - generateESM: false, - schemaPrefix: undefined + generateESM: false }; it('gets default options', () => { diff --git a/packages/openapi-generator/src/options/options.ts b/packages/openapi-generator/src/options/options.ts index 8090acd019..296890255a 100644 --- a/packages/openapi-generator/src/options/options.ts +++ b/packages/openapi-generator/src/options/options.ts @@ -15,11 +15,6 @@ export type GeneratorOptions = CommonGeneratorOptions & OpenAPIGeneratorOptions; * Options to configure OpenAPI client generation when using the generator programmatically. */ export interface OpenAPIGeneratorOptions { - /** - * Prefix all schema names with a value. - * @experimental - */ - schemaPrefix?: string; /** * Whether to generate ECMAScript modules instead of CommonJS modules. */ @@ -42,12 +37,5 @@ export const cliOptions = { 'When enabled, all generated files follow the ECMAScript module syntax.', type: 'boolean', default: false - }, - schemaPrefix: { - describe: - 'Prefix all schema names with a value. This is useful to avoid naming conflicts when multiple services are generated.', - type: 'string', - default: '', - hidden: true } } as const satisfies Options; diff --git a/packages/openapi-generator/src/parser/options.ts b/packages/openapi-generator/src/parser/options.ts index 51483b198a..ba000eebe4 100644 --- a/packages/openapi-generator/src/parser/options.ts +++ b/packages/openapi-generator/src/parser/options.ts @@ -7,8 +7,4 @@ export interface ParserOptions { * Fail parsing on conflicting names. */ strictNaming: boolean; - /** - * Add prefix to schema names. - */ - schemaPrefix: string; } diff --git a/packages/openapi-generator/src/parser/refs.spec.ts b/packages/openapi-generator/src/parser/refs.spec.ts index a400d961a5..a4f4d0c7a0 100644 --- a/packages/openapi-generator/src/parser/refs.spec.ts +++ b/packages/openapi-generator/src/parser/refs.spec.ts @@ -60,25 +60,6 @@ describe('OpenApiDocumentRefs', () => { ); }); - it('gets the schema naming for reference object with a prefix', async () => { - refs = await createRefs( - { - ...emptyDocument, - components: { schemas: { typeName } } - }, - { strictNaming: true, schemaPrefix: 'xyz' } - ); - - expect( - refs.getSchemaNaming({ - $ref: '#/components/schemas/typeName' - }) - ).toEqual({ - schemaName: 'XyzTypeName', - fileName: 'type-name' - }); - }); - it('renames a schema if needed due to illegal names', async () => { refs = await createRefs( { diff --git a/packages/openapi-generator/src/parser/refs.ts b/packages/openapi-generator/src/parser/refs.ts index 717111ab9a..188f4d9fa1 100644 --- a/packages/openapi-generator/src/parser/refs.ts +++ b/packages/openapi-generator/src/parser/refs.ts @@ -72,7 +72,7 @@ export class OpenApiDocumentRefs { (mapping, originalName, i) => ({ ...mapping, [`#/components/schemas/${originalName}`]: { - schemaName: pascalCase(options.schemaPrefix ?? '') + schemaNames[i], + schemaName: schemaNames[i], fileName: fileNames[i] } }), From 79646c211244fc886660cce220a38804bf92ac7c Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Thu, 12 Sep 2024 20:31:58 +0200 Subject: [PATCH 15/22] refactor + tests --- .../src/parser/schema.spec.ts | 92 +++++++++++++++++++ .../openapi-generator/src/parser/schema.ts | 24 +++-- .../schema/test-entity-2.d.ts | 3 +- .../schema/test-entity-2.ts | 3 +- .../schema/test-entity-3.d.ts | 8 +- .../schema/test-entity-3.ts | 8 +- .../swagger-yaml-service.yml | 5 +- 7 files changed, 125 insertions(+), 18 deletions(-) diff --git a/packages/openapi-generator/src/parser/schema.spec.ts b/packages/openapi-generator/src/parser/schema.spec.ts index 979a998aa4..64a1cf80ea 100644 --- a/packages/openapi-generator/src/parser/schema.spec.ts +++ b/packages/openapi-generator/src/parser/schema.spec.ts @@ -390,6 +390,98 @@ describe('parseSchema', () => { ); }); + it('parses xOf schema ignoring type:object at same level', async () => { + const schema: OpenAPIV3.SchemaObject = { + type: 'object', + oneOf: [ + { type: 'object' }, + { + anyOf: [ + { type: 'object' }, + { allOf: [{ type: 'object' }, { type: 'string' }] } + ] + } + ] + }; + expect(parseSchema(schema, await createTestRefs(), defaultOptions)).toEqual( + { + oneOf: [ + emptyObjectSchema, + { + anyOf: [ + emptyObjectSchema, + { allOf: [emptyObjectSchema, { type: 'string' }] } + ] + } + ] + } + ); + }); + + it('normalizes xOf schemas with properties at same level', async () => { + const schema: OpenAPIV3.SchemaObject = { + type: 'object', + properties: { prop1: { type: 'string' } }, + oneOf: [ + { type: 'object' }, + { + allOf: [{ type: 'object' }, { type: 'string' }] + } + ] + }; + expect(parseSchema(schema, await createTestRefs(), defaultOptions)).toEqual( + { + oneOf: [ + emptyObjectSchema, + { allOf: [emptyObjectSchema, { type: 'string' }] }, + { + additionalProperties: { type: 'any' }, + properties: [{ + name: 'prop1', + description: undefined, + required: false, + schema: { + type: 'string' + }, + schemaProperties: {} + }] + } + ] + } + ); + }); + + it('parses xOf schemas with required', async () => { + const schema: OpenAPIV3.SchemaObject = { + oneOf: [ + { type: 'object' }, + { + allOf: [{ type: 'object', properties: { prop1: { type: 'string' } } }, { type: 'string' }], + required: ['prop1'] + } + ] + }; + expect(parseSchema(schema, await createTestRefs(), defaultOptions)).toEqual( + { + oneOf: [ + emptyObjectSchema, + { allOf: [{ + additionalProperties: { type: 'any' }, + properties: [{ + name: 'prop1', + description: undefined, + required: true, + schema: { + type: 'string' + }, + schemaProperties: {} + }] + }, { type: 'string' }] }, + ] + } + ); + }); + it('parses not schema', async () => { const schema: OpenAPIV3.SchemaObject = { not: { type: 'object' } diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index df6c92ffdf..084df4f84c 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -51,13 +51,6 @@ export function parseSchema( } if (schema.allOf?.length) { - if (schema.properties) { - logger.info( - 'Detected schema with allOf and properties in the same level. This was refactored to a schema with allOf only, containing all the properties from the top level.' - ); - schema.allOf.push({ properties: schema.properties }); - delete schema.properties; - } return parseXOfSchema(schema, refs, 'allOf', options); } @@ -65,10 +58,12 @@ export function parseSchema( return parseXOfSchema(schema, refs, 'anyOf', options); } + // An object schema should be parsed after allOf, anyOf, oneOf. + // When object.properties are at the same level with anyOf, oneOf, allOf, they should be treated as part of allOf, etc. if ( schema.type === 'object' || schema.properties || - 'additionalProperties' in schema + schema.additionalProperties ) { return parseObjectSchema(schema, refs, options); } @@ -228,8 +223,19 @@ function parseXOfSchema( xOf: 'oneOf' | 'allOf' | 'anyOf', options: ParserOptions ): any { + let normalizedSchema: OpenAPIV3.NonArraySchemaObject = schema; + if (schema.properties || (schema.additionalProperties && typeof schema.additionalProperties === 'object')) { + logger.info( + `Detected schema with ${xOf} and properties in the same level. This was refactored to a schema with ${xOf} only, containing all the properties from the top level.` + ); + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const { [xOf]: _, ...objectSchema } = schema; + + normalizedSchema = { [xOf]: [...(schema[xOf] || []), objectSchema] }; + } return { - [xOf]: (schema[xOf] || []).map(entry => parseSchema(entry, refs, options)) + [xOf]: (normalizedSchema[xOf] || []).map(entry => parseSchema({ ...entry, + required: [...('required' in entry && entry.required ? entry.required : []), ...(normalizedSchema.required || [])] }, refs, options)) }; } diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts index 915f3964fe..5ff977cf29 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.d.ts @@ -8,5 +8,6 @@ import type { TestEntity } from './test-entity'; * Representation of the 'TestEntity2' schema. */ export type TestEntity2 = TestEntity & { - booleanProperty?: boolean; + booleanProperty: boolean; + integerProperty2?: number; } & Record; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts index 915f3964fe..5ff977cf29 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-2.ts @@ -8,5 +8,6 @@ import type { TestEntity } from './test-entity'; * Representation of the 'TestEntity2' schema. */ export type TestEntity2 = TestEntity & { - booleanProperty?: boolean; + booleanProperty: boolean; + integerProperty2?: number; } & Record; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.d.ts index 6664ba2494..dea9ce16a8 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.d.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.d.ts @@ -7,6 +7,8 @@ import type { TestEntity } from './test-entity'; /** * Representation of the 'TestEntity3' schema. */ -export type TestEntity3 = TestEntity & { - booleanProperty?: boolean; -} & Record; +export type TestEntity3 = + | TestEntity + | ({ + booleanProperty?: boolean; + } & Record); diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.ts index 6664ba2494..dea9ce16a8 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/test-entity-3.ts @@ -7,6 +7,8 @@ import type { TestEntity } from './test-entity'; /** * Representation of the 'TestEntity3' schema. */ -export type TestEntity3 = TestEntity & { - booleanProperty?: boolean; -} & Record; +export type TestEntity3 = + | TestEntity + | ({ + booleanProperty?: boolean; + } & Record); diff --git a/test-resources/openapi-service-specs/swagger-yaml-service.yml b/test-resources/openapi-service-specs/swagger-yaml-service.yml index 7db5d726f1..a760d9a112 100644 --- a/test-resources/openapi-service-specs/swagger-yaml-service.yml +++ b/test-resources/openapi-service-specs/swagger-yaml-service.yml @@ -61,11 +61,14 @@ definitions: - properties: booleanProperty: type: boolean + required: false + integerProperty2: + type: integer required: - booleanProperty TestEntity_3: type: object - allOf: + anyOf: - $ref: '#/definitions/TestEntity' properties: booleanProperty: From 7c8221bd0b65258b50dab2c64d76172f32a292ea Mon Sep 17 00:00:00 2001 From: cloud-sdk-js Date: Thu, 12 Sep 2024 18:34:27 +0000 Subject: [PATCH 16/22] Changes from lint:fix --- .../src/parser/schema.spec.ts | 58 +++++++++++-------- .../openapi-generator/src/parser/schema.ts | 21 ++++++- 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/packages/openapi-generator/src/parser/schema.spec.ts b/packages/openapi-generator/src/parser/schema.spec.ts index 64a1cf80ea..369060f657 100644 --- a/packages/openapi-generator/src/parser/schema.spec.ts +++ b/packages/openapi-generator/src/parser/schema.spec.ts @@ -425,7 +425,7 @@ describe('parseSchema', () => { oneOf: [ { type: 'object' }, { - allOf: [{ type: 'object' }, { type: 'string' }] + allOf: [{ type: 'object' }, { type: 'string' }] } ] }; @@ -436,15 +436,17 @@ describe('parseSchema', () => { { allOf: [emptyObjectSchema, { type: 'string' }] }, { additionalProperties: { type: 'any' }, - properties: [{ - name: 'prop1', - description: undefined, - required: false, - schema: { - type: 'string' - }, - schemaProperties: {} - }] + properties: [ + { + name: 'prop1', + description: undefined, + required: false, + schema: { + type: 'string' + }, + schemaProperties: {} + } + ] } ] } @@ -456,8 +458,11 @@ describe('parseSchema', () => { oneOf: [ { type: 'object' }, { - allOf: [{ type: 'object', properties: { prop1: { type: 'string' } } }, { type: 'string' }], - required: ['prop1'] + allOf: [ + { type: 'object', properties: { prop1: { type: 'string' } } }, + { type: 'string' } + ], + required: ['prop1'] } ] }; @@ -465,18 +470,25 @@ describe('parseSchema', () => { { oneOf: [ emptyObjectSchema, - { allOf: [{ - additionalProperties: { type: 'any' }, - properties: [{ - name: 'prop1', - description: undefined, - required: true, - schema: { - type: 'string' + { + allOf: [ + { + additionalProperties: { type: 'any' }, + properties: [ + { + name: 'prop1', + description: undefined, + required: true, + schema: { + type: 'string' + }, + schemaProperties: {} + } + ] }, - schemaProperties: {} - }] - }, { type: 'string' }] }, + { type: 'string' } + ] + } ] } ); diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index 084df4f84c..2cf7e9355d 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -224,7 +224,11 @@ function parseXOfSchema( options: ParserOptions ): any { let normalizedSchema: OpenAPIV3.NonArraySchemaObject = schema; - if (schema.properties || (schema.additionalProperties && typeof schema.additionalProperties === 'object')) { + if ( + schema.properties || + (schema.additionalProperties && + typeof schema.additionalProperties === 'object') + ) { logger.info( `Detected schema with ${xOf} and properties in the same level. This was refactored to a schema with ${xOf} only, containing all the properties from the top level.` ); @@ -234,8 +238,19 @@ function parseXOfSchema( normalizedSchema = { [xOf]: [...(schema[xOf] || []), objectSchema] }; } return { - [xOf]: (normalizedSchema[xOf] || []).map(entry => parseSchema({ ...entry, - required: [...('required' in entry && entry.required ? entry.required : []), ...(normalizedSchema.required || [])] }, refs, options)) + [xOf]: (normalizedSchema[xOf] || []).map(entry => + parseSchema( + { + ...entry, + required: [ + ...('required' in entry && entry.required ? entry.required : []), + ...(normalizedSchema.required || []) + ] + }, + refs, + options + ) + ) }; } From 847e2bcffeac3dd7654a5bcf276b5e5837496115 Mon Sep 17 00:00:00 2001 From: Marika Marszalkowski <868536+marikaner@users.noreply.github.com> Date: Mon, 16 Sep 2024 10:19:09 +0200 Subject: [PATCH 17/22] Update packages/openapi-generator/src/generator.ts --- packages/openapi-generator/src/generator.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/openapi-generator/src/generator.ts b/packages/openapi-generator/src/generator.ts index e6210887ff..5d63405120 100644 --- a/packages/openapi-generator/src/generator.ts +++ b/packages/openapi-generator/src/generator.ts @@ -264,9 +264,7 @@ async function generateService( const parsedOpenApiDocument = await parseOpenApiDocument( openApiDocument, serviceOptions, - { - strictNaming: !options.skipValidation - } + { strictNaming: !options.skipValidation } ); const serviceDir = resolve(options.outputDir, serviceOptions.directoryName); From 4a7c8861f750477e35569522e34c64a53bc1da6a Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Mon, 16 Sep 2024 13:13:59 +0200 Subject: [PATCH 18/22] regenerate with different name --- .../schema/entity-x-of-inheritance.d.ts | 13 +++++++++++++ .../schema/entity-x-of-inheritance.js | 3 +++ .../schema/entity-x-of-inheritance.js.map | 1 + .../schema/entity-x-of-inheritance.ts | 13 +++++++++++++ .../schema/entity-x-of-normalized-with-object.d.ts | 14 ++++++++++++++ .../schema/entity-x-of-normalized-with-object.js | 3 +++ .../entity-x-of-normalized-with-object.js.map | 1 + .../schema/entity-x-of-normalized-with-object.ts | 14 ++++++++++++++ .../swagger-yaml-service/schema/index.d.ts | 4 ++-- .../swagger-yaml-service/schema/index.js | 4 ++-- .../swagger-yaml-service/schema/index.js.map | 2 +- .../swagger-yaml-service/schema/index.ts | 4 ++-- .../openapi-service-specs/swagger-yaml-service.yml | 4 ++-- 13 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.d.ts create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.js create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.js.map create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.ts create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.js create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.js.map create mode 100644 test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.d.ts new file mode 100644 index 0000000000..477444d4d3 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.d.ts @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved. + * + * This is a generated file powered by the SAP Cloud SDK for JavaScript. + */ +import type { TestEntity } from './test-entity'; +/** + * Representation of the 'EntityXOfInheritance' schema. + */ +export type EntityXOfInheritance = TestEntity & { + booleanProperty: boolean; + integerProperty2?: number; +} & Record; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.js b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.js new file mode 100644 index 0000000000..aa09050fa8 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=entity-x-of-inheritance.js.map \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.js.map b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.js.map new file mode 100644 index 0000000000..880415eca5 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.js.map @@ -0,0 +1 @@ +{"version":3,"file":"entity-x-of-inheritance.js","sourceRoot":"","sources":["entity-x-of-inheritance.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.ts new file mode 100644 index 0000000000..477444d4d3 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.ts @@ -0,0 +1,13 @@ +/* + * Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved. + * + * This is a generated file powered by the SAP Cloud SDK for JavaScript. + */ +import type { TestEntity } from './test-entity'; +/** + * Representation of the 'EntityXOfInheritance' schema. + */ +export type EntityXOfInheritance = TestEntity & { + booleanProperty: boolean; + integerProperty2?: number; +} & Record; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts new file mode 100644 index 0000000000..a9a64e2c44 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved. + * + * This is a generated file powered by the SAP Cloud SDK for JavaScript. + */ +import type { TestEntity } from './test-entity'; +/** + * Representation of the 'EntityXOfNormalizedWithObject' schema. + */ +export type EntityXOfNormalizedWithObject = + | TestEntity + | ({ + booleanProperty?: boolean; + } & Record); diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.js b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.js new file mode 100644 index 0000000000..0ae92ecfd5 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.js @@ -0,0 +1,3 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +//# sourceMappingURL=entity-x-of-normalized-with-object.js.map \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.js.map b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.js.map new file mode 100644 index 0000000000..ff2d854265 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.js.map @@ -0,0 +1 @@ +{"version":3,"file":"entity-x-of-normalized-with-object.js","sourceRoot":"","sources":["entity-x-of-normalized-with-object.ts"],"names":[],"mappings":""} \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts new file mode 100644 index 0000000000..a9a64e2c44 --- /dev/null +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts @@ -0,0 +1,14 @@ +/* + * Copyright (c) 2024 SAP SE or an SAP affiliate company. All rights reserved. + * + * This is a generated file powered by the SAP Cloud SDK for JavaScript. + */ +import type { TestEntity } from './test-entity'; +/** + * Representation of the 'EntityXOfNormalizedWithObject' schema. + */ +export type EntityXOfNormalizedWithObject = + | TestEntity + | ({ + booleanProperty?: boolean; + } & Record); diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.d.ts index b736051202..d8b616eb11 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.d.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.d.ts @@ -4,5 +4,5 @@ * This is a generated file powered by the SAP Cloud SDK for JavaScript. */ export * from './test-entity'; -export * from './test-entity-2'; -export * from './test-entity-3'; +export * from './entity-x-of-inheritance'; +export * from './entity-x-of-normalized-with-object'; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js index 44ca42c316..e08a604085 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js @@ -20,6 +20,6 @@ Object.defineProperty(exports, "__esModule", { value: true }); * This is a generated file powered by the SAP Cloud SDK for JavaScript. */ __exportStar(require("./test-entity"), exports); -__exportStar(require("./test-entity-2"), exports); -__exportStar(require("./test-entity-3"), exports); +__exportStar(require("./entity-x-of-inheritance"), exports); +__exportStar(require("./entity-x-of-normalized-with-object"), exports); //# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js.map b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js.map index 206023d27c..b47a5bf948 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js.map +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.js.map @@ -1 +1 @@ -{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;GAIG;AACH,gDAA8B;AAC9B,kDAAgC;AAChC,kDAAgC"} \ No newline at end of file +{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA;;;;GAIG;AACH,gDAA8B;AAC9B,4DAA0C;AAC1C,uEAAqD"} \ No newline at end of file diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.ts index b736051202..d8b616eb11 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/index.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/index.ts @@ -4,5 +4,5 @@ * This is a generated file powered by the SAP Cloud SDK for JavaScript. */ export * from './test-entity'; -export * from './test-entity-2'; -export * from './test-entity-3'; +export * from './entity-x-of-inheritance'; +export * from './entity-x-of-normalized-with-object'; diff --git a/test-resources/openapi-service-specs/swagger-yaml-service.yml b/test-resources/openapi-service-specs/swagger-yaml-service.yml index a760d9a112..51bcb99d12 100644 --- a/test-resources/openapi-service-specs/swagger-yaml-service.yml +++ b/test-resources/openapi-service-specs/swagger-yaml-service.yml @@ -54,7 +54,7 @@ definitions: type: integer description: An integer property required: false - TestEntity_2: + EntityXOfInheritance: type: object allOf: - $ref: '#/definitions/TestEntity' @@ -66,7 +66,7 @@ definitions: type: integer required: - booleanProperty - TestEntity_3: + EntityXOfNormalizedWithObject: type: object anyOf: - $ref: '#/definitions/TestEntity' From 6c1898e5e289511ae483115883a8f941176fdd9c Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Mon, 16 Sep 2024 14:57:53 +0200 Subject: [PATCH 19/22] review remarks --- .../openapi-generator/src/parser/schema.ts | 33 +++++++++++-------- .../schema/entity-x-of-inheritance.d.ts | 2 +- .../schema/entity-x-of-inheritance.ts | 2 +- .../entity-x-of-normalized-with-object.d.ts | 4 +-- .../entity-x-of-normalized-with-object.ts | 4 +-- .../swagger-yaml-service.yml | 5 ++- 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index 2cf7e9355d..bf55e02977 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -223,20 +223,8 @@ function parseXOfSchema( xOf: 'oneOf' | 'allOf' | 'anyOf', options: ParserOptions ): any { - let normalizedSchema: OpenAPIV3.NonArraySchemaObject = schema; - if ( - schema.properties || - (schema.additionalProperties && - typeof schema.additionalProperties === 'object') - ) { - logger.info( - `Detected schema with ${xOf} and properties in the same level. This was refactored to a schema with ${xOf} only, containing all the properties from the top level.` - ); - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const { [xOf]: _, ...objectSchema } = schema; + const normalizedSchema = normalizeSchema(schema, xOf); - normalizedSchema = { [xOf]: [...(schema[xOf] || []), objectSchema] }; - } return { [xOf]: (normalizedSchema[xOf] || []).map(entry => parseSchema( @@ -254,6 +242,25 @@ function parseXOfSchema( }; } +function normalizeSchema( + schema: OpenAPIV3.NonArraySchemaObject, + xOf: 'oneOf' | 'allOf' | 'anyOf' +): OpenAPIV3.NonArraySchemaObject { + if ( + schema.properties || + (schema.additionalProperties && + typeof schema.additionalProperties === 'object') + ) { + logger.info( + `Detected schema with ${xOf} and properties in the same level. This was refactored to a schema with ${xOf} only, containing all the properties from the top level.` + ); + + const { [xOf]: xOfSchema = [], ...objectSchema } = schema; + return { [xOf]: [...xOfSchema, objectSchema] }; + } + return schema; +} + /** * Parse schema properties e.g. 'maxLength', 'minimum', etc. * @param schema - Original schema representing a ref or schema object. diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.d.ts index 477444d4d3..f48707f045 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.d.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.d.ts @@ -5,7 +5,7 @@ */ import type { TestEntity } from './test-entity'; /** - * Representation of the 'EntityXOfInheritance' schema. + * Composition of extended properties (inheritance) and schema-specific properties together with required attribute. */ export type EntityXOfInheritance = TestEntity & { booleanProperty: boolean; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.ts index 477444d4d3..f48707f045 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-inheritance.ts @@ -5,7 +5,7 @@ */ import type { TestEntity } from './test-entity'; /** - * Representation of the 'EntityXOfInheritance' schema. + * Composition of extended properties (inheritance) and schema-specific properties together with required attribute. */ export type EntityXOfInheritance = TestEntity & { booleanProperty: boolean; diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts index a9a64e2c44..1db68bb83b 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts @@ -5,10 +5,10 @@ */ import type { TestEntity } from './test-entity'; /** - * Representation of the 'EntityXOfNormalizedWithObject' schema. + * Entity with xOf and schema properties at same level. This is normalized to only xOf with schema properties inside xOf. */ export type EntityXOfNormalizedWithObject = | TestEntity | ({ - booleanProperty?: boolean; + booleanProperty: boolean; } & Record); diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts index a9a64e2c44..1db68bb83b 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts @@ -5,10 +5,10 @@ */ import type { TestEntity } from './test-entity'; /** - * Representation of the 'EntityXOfNormalizedWithObject' schema. + * Entity with xOf and schema properties at same level. This is normalized to only xOf with schema properties inside xOf. */ export type EntityXOfNormalizedWithObject = | TestEntity | ({ - booleanProperty?: boolean; + booleanProperty: boolean; } & Record); diff --git a/test-resources/openapi-service-specs/swagger-yaml-service.yml b/test-resources/openapi-service-specs/swagger-yaml-service.yml index 51bcb99d12..0901db295c 100644 --- a/test-resources/openapi-service-specs/swagger-yaml-service.yml +++ b/test-resources/openapi-service-specs/swagger-yaml-service.yml @@ -56,6 +56,7 @@ definitions: required: false EntityXOfInheritance: type: object + description: Composition of extended properties (inheritance) and schema-specific properties together with required attribute. allOf: - $ref: '#/definitions/TestEntity' - properties: @@ -68,9 +69,11 @@ definitions: - booleanProperty EntityXOfNormalizedWithObject: type: object + description: Entity with xOf and schema properties at same level. This is normalized to only xOf with schema properties inside xOf. anyOf: - $ref: '#/definitions/TestEntity' properties: booleanProperty: type: boolean - + required: + - booleanProperty From 4e0c3d08d144cc24a8bc8be5732673632638876a Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Mon, 16 Sep 2024 15:24:37 +0200 Subject: [PATCH 20/22] add comments --- packages/openapi-generator/src/parser/schema.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index bf55e02977..d50ce60fe7 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -231,7 +231,9 @@ function parseXOfSchema( { ...entry, required: [ + // Add required properties from the entry. ...('required' in entry && entry.required ? entry.required : []), + // Add required properties from the top level schema (xOf). ...(normalizedSchema.required || []) ] }, From e3c3f0a906b09bc7d1f738679bb217541c9d94bc Mon Sep 17 00:00:00 2001 From: deekshas8 Date: Mon, 16 Sep 2024 16:52:10 +0200 Subject: [PATCH 21/22] additionalprop --- packages/openapi-generator/src/parser/schema.ts | 3 +-- .../schema/entity-x-of-normalized-with-object.d.ts | 2 +- .../schema/entity-x-of-normalized-with-object.ts | 2 +- test-resources/openapi-service-specs/swagger-yaml-service.yml | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index d50ce60fe7..7f86a8e6d2 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -250,8 +250,7 @@ function normalizeSchema( ): OpenAPIV3.NonArraySchemaObject { if ( schema.properties || - (schema.additionalProperties && - typeof schema.additionalProperties === 'object') + (schema.additionalProperties) ) { logger.info( `Detected schema with ${xOf} and properties in the same level. This was refactored to a schema with ${xOf} only, containing all the properties from the top level.` diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts index 1db68bb83b..c2cb117d5a 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.d.ts @@ -5,7 +5,7 @@ */ import type { TestEntity } from './test-entity'; /** - * Entity with xOf and schema properties at same level. This is normalized to only xOf with schema properties inside xOf. + * Entity with xOf and schema properties at same level. This is normalized to only xOf with schema properties inside xOf. Also works if only additionalProperties true is used. */ export type EntityXOfNormalizedWithObject = | TestEntity diff --git a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts index 1db68bb83b..c2cb117d5a 100644 --- a/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts +++ b/test-packages/test-services-openapi/swagger-yaml-service/schema/entity-x-of-normalized-with-object.ts @@ -5,7 +5,7 @@ */ import type { TestEntity } from './test-entity'; /** - * Entity with xOf and schema properties at same level. This is normalized to only xOf with schema properties inside xOf. + * Entity with xOf and schema properties at same level. This is normalized to only xOf with schema properties inside xOf. Also works if only additionalProperties true is used. */ export type EntityXOfNormalizedWithObject = | TestEntity diff --git a/test-resources/openapi-service-specs/swagger-yaml-service.yml b/test-resources/openapi-service-specs/swagger-yaml-service.yml index 0901db295c..e0cb547533 100644 --- a/test-resources/openapi-service-specs/swagger-yaml-service.yml +++ b/test-resources/openapi-service-specs/swagger-yaml-service.yml @@ -69,7 +69,7 @@ definitions: - booleanProperty EntityXOfNormalizedWithObject: type: object - description: Entity with xOf and schema properties at same level. This is normalized to only xOf with schema properties inside xOf. + description: Entity with xOf and schema properties at same level. This is normalized to only xOf with schema properties inside xOf. Also works if only additionalProperties true is used. anyOf: - $ref: '#/definitions/TestEntity' properties: From 05716de13fe11785cae6b1c9aedcb84a518d03b6 Mon Sep 17 00:00:00 2001 From: cloud-sdk-js Date: Mon, 16 Sep 2024 14:54:45 +0000 Subject: [PATCH 22/22] Changes from lint:fix --- packages/openapi-generator/src/parser/schema.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/openapi-generator/src/parser/schema.ts b/packages/openapi-generator/src/parser/schema.ts index 7f86a8e6d2..3c7349ed05 100644 --- a/packages/openapi-generator/src/parser/schema.ts +++ b/packages/openapi-generator/src/parser/schema.ts @@ -248,10 +248,7 @@ function normalizeSchema( schema: OpenAPIV3.NonArraySchemaObject, xOf: 'oneOf' | 'allOf' | 'anyOf' ): OpenAPIV3.NonArraySchemaObject { - if ( - schema.properties || - (schema.additionalProperties) - ) { + if (schema.properties || schema.additionalProperties) { logger.info( `Detected schema with ${xOf} and properties in the same level. This was refactored to a schema with ${xOf} only, containing all the properties from the top level.` );