Skip to content

Commit

Permalink
fix(schematics-sdk): rollback default spec file name to swagger-spec.…
Browse files Browse the repository at this point in the history
…yaml (#1737)

## Proposed change

Rollback default spec file name created when generating the SDK to
swagger-spec.yaml.
  • Loading branch information
kpanot committed May 2, 2024
2 parents b963e18 + e8bbc3a commit 8e8555b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Tree } from '@angular-devkit/schematics';
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import * as path from 'node:path';
import { LOCAL_SPEC_FILENAME, SPEC_JSON_EXTENSION, SPEC_YAML_EXTENSION } from './index';

const collectionPath = path.join(__dirname, '..', '..', '..', 'collection.json');

Expand Down Expand Up @@ -34,8 +35,8 @@ describe('Typescript Core Generator', () => {
}, baseTree);
const content: any = tree.readJson('/openapitools.json');

expect(content['generator-cli'].generators['test-sdk-sdk'].inputSpec.endsWith('openapi.yaml')).toBe(true);
expect(tree.exists('/openapi.yaml')).toBe(true);
expect(content['generator-cli'].generators['test-sdk-sdk'].inputSpec.endsWith(`${LOCAL_SPEC_FILENAME}.${SPEC_YAML_EXTENSION}`)).toBe(true);
expect(tree.exists(`/${LOCAL_SPEC_FILENAME}.${SPEC_YAML_EXTENSION}`)).toBe(true);
});

it('should update openapitools file with json', async () => {
Expand All @@ -45,8 +46,8 @@ describe('Typescript Core Generator', () => {
}, baseTree);
const content: any = tree.readJson('/openapitools.json');

expect(content['generator-cli'].generators['test-sdk-sdk'].inputSpec.endsWith('openapi.json')).toBe(true);
expect(tree.exists('/openapi.json')).toBe(true);
expect(content['generator-cli'].generators['test-sdk-sdk'].inputSpec.endsWith(`${LOCAL_SPEC_FILENAME}.${SPEC_JSON_EXTENSION}`)).toBe(true);
expect(tree.exists(`/${LOCAL_SPEC_FILENAME}.${SPEC_JSON_EXTENSION}`)).toBe(true);
});

it('should clean previous install', async () => {
Expand Down
16 changes: 12 additions & 4 deletions packages/@ama-sdk/schematics/schematics/typescript/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ import { generateOperationFinderFromSingleFile } from './helpers/path-extractor'

const JAVA_OPTIONS = ['specPath', 'specConfigPath', 'globalProperty', 'outputPath'];
const OPEN_API_TOOLS_OPTIONS = ['generatorName', 'output', 'inputSpec', 'config', 'globalProperty'];
const LOCAL_SPEC_FILENAME = 'openapi';

// TODO: Change to `open-api` when #1735 is done
/** Name of the specification file copied locally (without extension) */
export const LOCAL_SPEC_FILENAME = 'swagger-spec';
/** Extension of the Specification file in YAML format */
export const SPEC_YAML_EXTENSION = 'yaml';
// TODO: Change to `json` when #1735 is done
/** Extension of the Specification file in JSON format */
export const SPEC_JSON_EXTENSION = 'yaml';

interface OpenApiToolsGenerator {
/** Location of the OpenAPI spec, as URL or file */
Expand Down Expand Up @@ -171,8 +179,8 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKCoreSchematic
const targetPath = options.directory || '';
const generatorOptions = getGeneratorOptions(tree, context, options);
let isJson = false;
let specDefaultPath = path.posix.join(targetPath, `${LOCAL_SPEC_FILENAME}.json`);
specDefaultPath = existsSync(specDefaultPath) ? specDefaultPath : path.posix.join(targetPath, `${LOCAL_SPEC_FILENAME}.yaml`);
let specDefaultPath = path.posix.join(targetPath, `${LOCAL_SPEC_FILENAME}.${SPEC_JSON_EXTENSION}`);
specDefaultPath = existsSync(specDefaultPath) ? specDefaultPath : path.posix.join(targetPath, `${LOCAL_SPEC_FILENAME}.${SPEC_YAML_EXTENSION}`);
generatorOptions.specPath ||= specDefaultPath;

let specContent!: string;
Expand All @@ -188,7 +196,7 @@ function ngGenerateTypescriptSDKFn(options: NgGenerateTypescriptSDKCoreSchematic
} catch (e) {
isJson = false;
}
const defaultFileName = `${LOCAL_SPEC_FILENAME}.${isJson ? 'json' : 'yaml'}`;
const defaultFileName = `${LOCAL_SPEC_FILENAME}.${isJson ? SPEC_JSON_EXTENSION : SPEC_YAML_EXTENSION}`;
specDefaultPath = path.posix.join(targetPath, defaultFileName);
generatorOptions.specPath = specDefaultPath;

Expand Down

0 comments on commit 8e8555b

Please sign in to comment.