Skip to content

Commit 6cc07bc

Browse files
authored
feat: Support resolve external option (#5360)
1 parent 0aad598 commit 6cc07bc

21 files changed

+177
-87
lines changed

.changeset/stale-plums-sneeze.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sap-cloud-sdk/openapi-generator': minor
3+
---
4+
5+
[New Functionality] Add `resolveExternal` option to determine whether external $ref pointers will be resolved.

packages/openapi-generator/src/generator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ async function generateService(
272272
serviceOptions,
273273
{
274274
strictNaming: !options.skipValidation,
275-
schemaPrefix: options.schemaPrefix
275+
schemaPrefix: options.schemaPrefix,
276+
resolveExternal: options.resolveExternal
276277
}
277278
);
278279

packages/openapi-generator/src/options/generator-options.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ describe('parseGeneratorOptions', () => {
3838
overwrite: false,
3939
config: undefined,
4040
generateESM: false,
41-
schemaPrefix: ''
41+
schemaPrefix: '',
42+
resolveExternal: true
4243
};
4344

4445
it('gets default options', () => {

packages/openapi-generator/src/options/options.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export interface OpenAPIGeneratorOptions {
2323
* @experimental
2424
*/
2525
schemaPrefix?: string;
26+
/**
27+
* Resolve external references.
28+
*/
29+
resolveExternal?: boolean;
2630
}
2731

2832
/**
@@ -48,5 +52,12 @@ export const cliOptions = {
4852
type: 'string',
4953
default: '',
5054
hidden: true
55+
},
56+
resolveExternal: {
57+
describe:
58+
'By default, external $ref pointers will be resolved. If set to false, external $ref pointers will be ignored.',
59+
type: 'boolean',
60+
default: true,
61+
hidden: true
5162
}
5263
} as const satisfies Options<GeneratorOptions>;

packages/openapi-generator/src/parser/api.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { parseApis } from './api';
44
import { createRefs } from './refs';
55
import type { OpenAPIV3 } from 'openapi-types';
66

7-
const options = { strictNaming: true, schemaPrefix: '' };
7+
const options = { strictNaming: true, schemaPrefix: '', resolveExternal: true };
88
describe('parseApis', () => {
99
it('throws an error if there are APIs without paths', async () => {
1010
const refs = await createTestRefs();

packages/openapi-generator/src/parser/document.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as api from './api';
44
import type { ServiceOptions } from '@sap-cloud-sdk/generator-common/dist/options-per-service';
55
import type { OpenAPIV3 } from 'openapi-types';
66

7-
const options = { strictNaming: true, schemaPrefix: '' };
7+
const options = { strictNaming: true, schemaPrefix: '', resolveExternal: true };
88
describe('parseOpenApiDocument()', () => {
99
it('does not modify input service specification', () => {
1010
const input: OpenAPIV3.Document = {
@@ -54,7 +54,7 @@ describe('parseOpenApiDocument()', () => {
5454
packageName: '@sap/cloud-sdk-openapi-test-service',
5555
directoryName: 'test-service'
5656
},
57-
{ strictNaming: false, schemaPrefix: '' }
57+
{ strictNaming: false, schemaPrefix: '', resolveExternal: true }
5858
);
5959

6060
expect(parsedDocument.schemas).toEqual([
@@ -276,7 +276,7 @@ describe('parseOpenApiDocument()', () => {
276276
packageName: '@sap/cloud-sdk-openapi-test-service',
277277
directoryName: 'test-service'
278278
},
279-
{ strictNaming: false, schemaPrefix: '' }
279+
{ strictNaming: false, schemaPrefix: '', resolveExternal: true }
280280
);
281281

282282
expect(parsedDocument.schemas).toEqual([

packages/openapi-generator/src/parser/document.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import SwaggerParser from '@apidevtools/swagger-parser';
12
import {
23
isAllOfSchema,
34
isOneOfSchema,
@@ -10,7 +11,6 @@ import {
1011
} from './schema';
1112
import { parseApis } from './api';
1213
import { createRefs } from './refs';
13-
import { parseBound } from './swagger-parser-workaround';
1414
import type { OpenAPIV3 } from 'openapi-types';
1515
import type { ServiceOptions } from '@sap-cloud-sdk/generator-common/internal';
1616
import type {
@@ -35,7 +35,9 @@ export async function parseOpenApiDocument(
3535
options: ParserOptions
3636
): Promise<OpenApiDocument> {
3737
const clonedContent = JSON.parse(JSON.stringify(fileContent));
38-
const document = (await parseBound(clonedContent)) as OpenAPIV3.Document;
38+
const document = (await SwaggerParser.parse(
39+
clonedContent
40+
)) as OpenAPIV3.Document;
3941
const refs = await createRefs(document, options);
4042
const schemas = parseSchemas(document, refs, options);
4143
sanitizeDiscriminatedSchemas(schemas, refs, options);

packages/openapi-generator/src/parser/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@ export * from './request-body';
1010
export * from './responses';
1111
export * from './schema-naming';
1212
export * from './schema';
13-
export * from './swagger-parser-workaround';
1413
export * from './type-mapping';
1514
export * from './unique-naming';

packages/openapi-generator/src/parser/media-type.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { createTestRefs, emptyObjectSchema } from '../../test/test-util';
22
import { parseTopLevelMediaType, parseMediaType } from './media-type';
33

4-
const defaultOptions = { strictNaming: true, schemaPrefix: '' };
4+
const defaultOptions = {
5+
strictNaming: true,
6+
schemaPrefix: '',
7+
resolveExternal: true
8+
};
59
describe('parseTopLevelMediaType', () => {
610
it('returns undefined if the media type is not supported', async () => {
711
expect(

packages/openapi-generator/src/parser/operation.spec.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import {
88
import type { OpenAPIV3 } from 'openapi-types';
99
import type { OpenApiParameter } from '../openapi-types';
1010

11-
const defaultOptions = { strictNaming: true, schemaPrefix: '' };
11+
const defaultOptions = {
12+
strictNaming: true,
13+
schemaPrefix: '',
14+
resolveExternal: true
15+
};
1216
describe('getRelevantParameters', () => {
1317
it('ignores cookie parameters', async () => {
1418
expect(
@@ -111,7 +115,8 @@ describe('parsePathParameters', () => {
111115
expect(
112116
parsePathParameters([], await createTestRefs(), {
113117
strictNaming: false,
114-
schemaPrefix: ''
118+
schemaPrefix: '',
119+
resolveExternal: true
115120
})
116121
).toEqual([]);
117122
});
@@ -131,7 +136,8 @@ describe('parsePathParameters', () => {
131136
expect(
132137
parsePathParameters([pathParam1, pathParam2], refs, {
133138
strictNaming: false,
134-
schemaPrefix: ''
139+
schemaPrefix: '',
140+
resolveExternal: true
135141
})
136142
).toEqual([
137143
{ ...pathParam1, originalName: 'param1', schemaProperties: {} },

0 commit comments

Comments
 (0)