@oslo-flanders/swagger-generator 1.1.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -37,6 +37,7 @@ The service is executed from the CLI and expects the following parameters:
37
37
  | `--licenseURL` | URL of the license of the API | No | |
38
38
  | `--excludeClasses` | Classes to exclude from the generated Swagger | No | Persoon Organisatie Test |
39
39
  | `--excludeProperties` | Properties to exclude from the generated Swagger | No | voornaam achternaam |
40
+ | `--outputFormat` | Output format for the generated files. Can be specified multiple times to generate multiple formats at once. | No | `application/json` (default) or `application/yaml` |
40
41
  | `--silent` | Suppress log messages | No | `true` or `false` (default) |
41
42
 
42
43
  ## Usage
@@ -46,4 +47,6 @@ oslo-generator-swagger --input report.jsonld --output swagger.json --language nl
46
47
  oslo-generator-swagger --input report.jsonld --output swagger.json --language nl --versionSwagger 3.0.4 --versionAPI 1.0.0 --title "Mijn API" --description "Mijn API beschrijving." --contextURL http://example.com/context.jsonld --baseURL http://example.com --excludeClasses Persoon Organisatie
47
48
  oslo-generator-swagger --input report.jsonld --output swagger.json --language nl --versionSwagger 3.0.4 --versionAPI 1.0.0 --title "Mijn API" --description "Mijn API beschrijving." --contextURL http://example.com/context.jsonld --baseURL http://example.com --excludeClasses Persoon --excludeProperties Persoon.voornaam
48
49
  oslo-generator-swagger --input report.jsonld --output swagger.json --language nl --versionSwagger 3.0.4 --versionAPI 1.0.0 --title "Mijn API" --description "Mijn API beschrijving." --contextURL http://example.com/context.jsonld --baseURL http://example.com --excludeClasses GeregistreerdPersoon --excludeProperties Persoon.voornaam Persoon.achternaam
50
+ oslo-generator-swagger --input report.jsonld --output swagger.yaml --language nl --versionSwagger 3.0.4 --versionAPI 1.0.0 --title "Mijn API" --description "Mijn API beschrijving." --contextURL http://example.com/context.jsonld --baseURL http://example.com --outputFormat application/yaml
51
+ oslo-generator-swagger --input report.jsonld --output swagger --language nl --versionSwagger 3.0.4 --versionAPI 1.0.0 --title "Mijn API" --description "Mijn API beschrijving." --contextURL http://example.com/context.jsonld --baseURL http://example.com --outputFormat application/json application/yaml
49
52
  ```
@@ -1,5 +1,5 @@
1
1
  import type { IService } from '@oslo-flanders/core';
2
- import { QuadStore, Logger } from '@oslo-flanders/core';
2
+ import { QuadStore, Logger, OutputFormat } from '@oslo-flanders/core';
3
3
  import { SwaggerGenerationServiceConfiguration } from './config/SwaggerGenerationServiceConfiguration';
4
4
  export declare class SwaggerGenerationService implements IService {
5
5
  readonly logger: Logger;
@@ -10,10 +10,11 @@ export declare class SwaggerGenerationService implements IService {
10
10
  private getProbleemdetailsLink;
11
11
  private getContact;
12
12
  private getLicense;
13
+ private schemaExists;
13
14
  init(): Promise<void>;
14
15
  run(): Promise<void>;
15
16
  createSwagger(schemas: any, links: any): Object;
16
17
  createSchemas(): Object;
17
18
  createLinks(): Object;
18
- writeJSON(json: Object, outputPath: string): Promise<void>;
19
+ writeOutput(outputFormat: OutputFormat, obj: Object, outputPath: string): Promise<void>;
19
20
  }
@@ -39,9 +39,14 @@ exports.SwaggerGenerationService = void 0;
39
39
  const core_1 = require("@oslo-flanders/core");
40
40
  const path = __importStar(require("path"));
41
41
  const promises_1 = require("fs/promises");
42
+ const yaml = __importStar(require("js-yaml"));
42
43
  const inversify_1 = require("inversify");
43
44
  const SwaggerGenerationServiceConfiguration_1 = require("./config/SwaggerGenerationServiceConfiguration");
44
45
  const Properties_1 = require("./enums/Properties");
46
+ const FILE_EXTENSIONS = {
47
+ [core_1.OutputFormat.Json]: '.json',
48
+ [core_1.OutputFormat.Yaml]: '.yaml',
49
+ };
45
50
  let SwaggerGenerationService = class SwaggerGenerationService {
46
51
  constructor(logger, config, store) {
47
52
  this.logger = logger;
@@ -89,18 +94,23 @@ let SwaggerGenerationService = class SwaggerGenerationService {
89
94
  url: this.configuration.licenseURL,
90
95
  };
91
96
  }
97
+ /* The schema already exists in the model. This would mean that there are identical labels, which isn't ideal
98
+ https://vlaamseoverheid.atlassian.net/browse/DATAST-2249?atlOrigin=eyJpIjoiMWYyZjVmOTFhMWExNDc4MmIwMGQxNjhkMjJmN2NhZGUiLCJwIjoiaiJ9
99
+ */
100
+ schemaExists(schema, label) {
101
+ if (schema[label]) {
102
+ this.logger.warn(`[SwaggerGenerationService]: Schema already exists for the label (${label}) and will be overwritten.`);
103
+ }
104
+ return !!schema[label];
105
+ }
92
106
  async init() {
93
107
  return this.store.addQuadsFromFile(this.configuration.input);
94
108
  }
95
109
  async run() {
96
- /* Create Swagger schemas */
110
+ var _a;
111
+ /* Create schemas and links once, then write for each output format */
97
112
  const schemas = this.createSchemas();
98
- for (const label of Object.keys(schemas))
99
- await this.writeJSON(schemas[label], `swagger/components/schemas/${label}.json`);
100
- /* Create Swagger links */
101
113
  const links = this.createLinks();
102
- for (const label of Object.keys(links))
103
- await this.writeJSON(links[label], `swagger/components/links/${label}.json`);
104
114
  /* Create self-standing referenceable components */
105
115
  const components = {
106
116
  openapi: this.configuration.versionSwagger,
@@ -113,10 +123,17 @@ let SwaggerGenerationService = class SwaggerGenerationService {
113
123
  },
114
124
  components: { schemas, links },
115
125
  };
116
- await this.writeJSON(components, `swagger/components.json`);
117
126
  /* Create Swagger endpoint paths as example */
118
127
  const swagger = this.createSwagger(schemas, links);
119
- await this.writeJSON(swagger, 'swagger/example.json');
128
+ for (const format of this.configuration.outputFormat) {
129
+ const ext = (_a = FILE_EXTENSIONS[format]) !== null && _a !== void 0 ? _a : '.json';
130
+ for (const label of Object.keys(schemas))
131
+ await this.writeOutput(format, schemas[label], `swagger/components/schemas/${label}${ext}`);
132
+ for (const label of Object.keys(links))
133
+ await this.writeOutput(format, links[label], `swagger/components/links/${label}${ext}`);
134
+ await this.writeOutput(format, components, `swagger/components${ext}`);
135
+ await this.writeOutput(format, swagger, `swagger/example${ext}`);
136
+ }
120
137
  }
121
138
  createSwagger(schemas, links) {
122
139
  var _a, _b, _c;
@@ -252,7 +269,7 @@ let SwaggerGenerationService = class SwaggerGenerationService {
252
269
  var _a, _b, _c, _d, _e, _f, _g, _h, _j;
253
270
  const schemas = {};
254
271
  /* Create schema for each enumeration */
255
- for (const enumId of this.store.findSubjects(core_1.ns.oslo('assignedURI'), core_1.ns.skos('Concept'))) {
272
+ for (const enumId of this.store.getEnumerations()) {
256
273
  let label = (_a = (0, core_1.getApplicationProfileLabel)(enumId, this.store, this.configuration.language)) === null || _a === void 0 ? void 0 : _a.value;
257
274
  if (!label) {
258
275
  this.logger.error(`Unknown enum label for ${enumId.value}`);
@@ -260,6 +277,7 @@ let SwaggerGenerationService = class SwaggerGenerationService {
260
277
  }
261
278
  /* Class labels should be always pascal cased */
262
279
  label = (0, core_1.toPascalCase)(label);
280
+ this.schemaExists(schemas, label);
263
281
  schemas[label] = {
264
282
  title: label,
265
283
  type: 'object',
@@ -277,7 +295,9 @@ let SwaggerGenerationService = class SwaggerGenerationService {
277
295
  for (const classId of [
278
296
  ...this.store.findSubjects(core_1.ns.rdf('type'), core_1.ns.owl('Class')),
279
297
  ...this.store.findSubjects(core_1.ns.rdf('type'), core_1.ns.rdfs('Datatype')),
280
- ]) {
298
+ ]
299
+ // Extra filter to exclude all the enumerations since these are mentioned twice in the intermedairy format under enums and classes
300
+ .filter((val) => !(0, core_1.isEnumeration)(this.store, val))) {
281
301
  const assignedUri = this.store.getAssignedUri(classId);
282
302
  /* Primitive datatypes may not be generated */
283
303
  if (assignedUri && [...core_1.DataTypes.values()].includes(assignedUri.value))
@@ -412,6 +432,7 @@ let SwaggerGenerationService = class SwaggerGenerationService {
412
432
  },
413
433
  };
414
434
  }
435
+ this.schemaExists(schemas, label);
415
436
  /* Create components for each schema */
416
437
  schemas[label] = {
417
438
  title: label,
@@ -503,8 +524,11 @@ let SwaggerGenerationService = class SwaggerGenerationService {
503
524
  }
504
525
  return links;
505
526
  }
506
- async writeJSON(json, outputPath) {
507
- const data = JSON.stringify(json, null, 2);
527
+ async writeOutput(outputFormat, obj, outputPath) {
528
+ const isYaml = outputFormat === core_1.OutputFormat.Yaml;
529
+ const data = isYaml
530
+ ? yaml.dump(JSON.parse(JSON.stringify(obj)), { noRefs: true, lineWidth: -1 })
531
+ : JSON.stringify(obj, null, 2);
508
532
  const filePath = path.join(this.configuration.output, outputPath);
509
533
  (0, core_1.ensureOutputDirectory)(path.dirname(filePath));
510
534
  await (0, promises_1.writeFile)(filePath, data);
@@ -46,6 +46,12 @@ class SwaggerGenerationServiceRunner extends core_1.AppRunner {
46
46
  describe: 'All logs are suppressed',
47
47
  default: false,
48
48
  boolean: true,
49
+ })
50
+ .option('outputFormat', {
51
+ describe: 'Output format for the generated files. Can be specified multiple times.',
52
+ type: 'string',
53
+ array: true,
54
+ default: [core_1.OutputFormat.Json],
49
55
  })
50
56
  .demandOption([
51
57
  'input',
@@ -1,4 +1,5 @@
1
1
  import type { IConfiguration, YargsParams } from '@oslo-flanders/core';
2
+ import { OutputFormat } from '@oslo-flanders/core';
2
3
  export declare class SwaggerGenerationServiceConfiguration implements IConfiguration {
3
4
  /**
4
5
  * Local path or URL to (OSLO compliant) RDF file to generate JSON-LD context from
@@ -64,6 +65,10 @@ export declare class SwaggerGenerationServiceConfiguration implements IConfigura
64
65
  * An array of property names to be excluded from the generated output
65
66
  */
66
67
  private _excludeProperties;
68
+ /**
69
+ * Output formats
70
+ */
71
+ private _outputFormat;
67
72
  createFromCli(params: YargsParams): Promise<void>;
68
73
  get input(): string;
69
74
  get output(): string;
@@ -81,4 +86,5 @@ export declare class SwaggerGenerationServiceConfiguration implements IConfigura
81
86
  get licenseURL(): string | undefined;
82
87
  get excludeClasses(): string[];
83
88
  get excludeProperties(): string[];
89
+ get outputFormat(): OutputFormat[];
84
90
  }
@@ -7,6 +7,7 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
7
7
  };
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.SwaggerGenerationServiceConfiguration = void 0;
10
+ const core_1 = require("@oslo-flanders/core");
10
11
  const inversify_1 = require("inversify");
11
12
  let SwaggerGenerationServiceConfiguration = class SwaggerGenerationServiceConfiguration {
12
13
  async createFromCli(params) {
@@ -26,6 +27,7 @@ let SwaggerGenerationServiceConfiguration = class SwaggerGenerationServiceConfig
26
27
  this._licenseURL = params.licenseURL;
27
28
  this._excludeClasses = params.excludeClasses;
28
29
  this._excludeProperties = params.excludeProperties;
30
+ this._outputFormat = params.outputFormat;
29
31
  }
30
32
  get input() {
31
33
  if (!this._input) {
@@ -99,6 +101,9 @@ let SwaggerGenerationServiceConfiguration = class SwaggerGenerationServiceConfig
99
101
  get excludeProperties() {
100
102
  return this._excludeProperties || [];
101
103
  }
104
+ get outputFormat() {
105
+ return this._outputFormat || [core_1.OutputFormat.Json];
106
+ }
102
107
  };
103
108
  SwaggerGenerationServiceConfiguration = __decorate([
104
109
  (0, inversify_1.injectable)()
@@ -30,6 +30,7 @@ const Properties = new Map([
30
30
  [core_1.ns.xsd('unsignedShort').value, { '@value': { type: 'number', format: 'int32', minimum: 0, maximum: 65535 }, '@type': { type: 'string', pattern: '^UnsignedShort$' } }],
31
31
  [core_1.ns.xsd('unsignedByte').value, { '@value': { type: 'number', format: 'int32', minimum: 0, maximum: 255 }, '@type': { type: 'string', pattern: '^UnsignedByte$' } }],
32
32
  [core_1.ns.xsd('decimal').value, { '@value': { type: 'string', pattern: '(+|-)?([0-9]+(.[0-9]*)?|.[0-9]+)' }, '@type': { type: 'string', pattern: '^Decimal$' } }],
33
+ [core_1.ns.xsd('language').value, { '@value': { type: 'string', pattern: '^[a-z]{2,3}(-[A-Z]{2})?' }, '@type': { type: 'string', pattern: '^Language$' } }],
33
34
  ]);
34
35
  /* eslint-enable max-len*/
35
36
  const mapProperties = (datatype, label, subclasses, abstract, excludeClasses) => {
@@ -91,3 +91,6 @@ export interface SwaggerLink {
91
91
  parameters: Record<string, string>;
92
92
  description: string;
93
93
  }
94
+ export interface Schema {
95
+ [key: string]: any;
96
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslo-flanders/swagger-generator",
3
- "version": "1.1.1",
3
+ "version": "1.3.0",
4
4
  "description": "Generate OpenAPI Swagger YAML documentation.",
5
5
  "author": "Digitaal Vlaanderen <https://data.vlaanderen.be/id/organisatie/OVO002949>",
6
6
  "homepage": "https://github.com/informatievlaanderen/OSLO-UML-Transformer/tree/main/packages/oslo-generator-swagger#readme",
@@ -37,11 +37,13 @@
37
37
  },
38
38
  "devDependencies": {
39
39
  "@rdfjs/types": "^2.0.0",
40
+ "@types/js-yaml": "^4.0.0",
40
41
  "@types/streamify-array": "^1.0.0"
41
42
  },
42
43
  "dependencies": {
43
- "@oslo-flanders/core": "^1.1.0",
44
+ "@oslo-flanders/core": "^1.2.0",
44
45
  "inversify": "^6.0.1",
46
+ "js-yaml": "^4.1.0",
45
47
  "n3": "^2.0.0",
46
48
  "rdf-data-factory": "^2.0.0",
47
49
  "rdf-parse": "^5.0.0",