@goast/kotlin 0.3.1 → 0.3.3

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.
@@ -14,24 +14,6 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
14
14
  // Do not generate types that are only used for anyOf and/or allOf
15
15
  return { type: ast_1.kt.refs.any({ nullable: true }) };
16
16
  }
17
- if (ctx.schema.id === ctx.schema.name) {
18
- // TODO: Add this to @goast/core
19
- const match = ctx.schema.$src.path.match(/\/components\/responses\/([^/]+)\/content\/.+\/schema/);
20
- if (match) {
21
- ctx.schema.name = match[1].toLowerCase().endsWith('response') ? match[1] : match[1] + 'Response';
22
- }
23
- }
24
- if (ctx.schema.isNameGenerated) {
25
- // TODO: Change this in @goast/core
26
- const match = ctx.schema.$src.path.match(/\/paths\/(?<path>.+)\/(?<method>.+)\/responses\/(?<status>\d+)\//);
27
- if (match && match.groups) {
28
- const { path, method, status } = match.groups;
29
- const endpoint = ctx.data.endpoints.find((e) => e.path === path && e.method === method);
30
- if (endpoint) {
31
- ctx.schema.name = `${endpoint.name}${status}Response`;
32
- }
33
- }
34
- }
35
17
  if (this.shouldGenerateTypeDeclaration(ctx, { schema: ctx.schema })) {
36
18
  const typeName = this.getDeclarationTypeName(ctx, { schema: ctx.schema });
37
19
  const packageName = this.getPackageName(ctx, { schema: ctx.schema });
@@ -187,6 +169,8 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
187
169
  return schema.enum && schema.enum.length > 0
188
170
  ? ast_1.kt.call([this.getType(ctx, { schema }), (0, core_1.toCasing)(String(schema.default), ctx.config.enumValueNameCasing)])
189
171
  : ast_1.kt.string(String(schema.default));
172
+ case 'array':
173
+ return ast_1.kt.call(ast_1.kt.refs.listOf.infer(), Array.isArray(schema.default) ? schema.default.map((x) => ast_1.kt.toNode(x)) : []);
190
174
  default:
191
175
  return 'null';
192
176
  }
@@ -361,6 +345,9 @@ class DefaultKotlinModelGenerator extends file_generator_1.KotlinFileGenerator {
361
345
  if (schema.kind === 'object' && schema.properties.size === 0 && schema.additionalProperties) {
362
346
  return false;
363
347
  }
348
+ if (schema.kind === 'object' && ctx.config.emptyObjectTypeBehavior === 'use-any' && schema.properties.size === 0) {
349
+ return false;
350
+ }
364
351
  // Dynamically generated schemas do not have its own type declaration
365
352
  if (!ctx.data.schemas.some((x) => x.id === schema.id)) {
366
353
  return false;
@@ -7,6 +7,7 @@ exports.defaultKotlinModelsGeneratorConfig = {
7
7
  packageName: 'com.openapi.generated',
8
8
  packageSuffix: '.model',
9
9
  oneOfBehavior: 'treat-as-any-of',
10
+ emptyObjectTypeBehavior: 'generate-empty-class',
10
11
  addJacksonAnnotations: true,
11
12
  addJakartaValidationAnnotations: true,
12
13
  addSwaggerAnnotations: true,
@@ -11,24 +11,6 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
11
11
  // Do not generate types that are only used for anyOf and/or allOf
12
12
  return { type: kt.refs.any({ nullable: true }) };
13
13
  }
14
- if (ctx.schema.id === ctx.schema.name) {
15
- // TODO: Add this to @goast/core
16
- const match = ctx.schema.$src.path.match(/\/components\/responses\/([^/]+)\/content\/.+\/schema/);
17
- if (match) {
18
- ctx.schema.name = match[1].toLowerCase().endsWith('response') ? match[1] : match[1] + 'Response';
19
- }
20
- }
21
- if (ctx.schema.isNameGenerated) {
22
- // TODO: Change this in @goast/core
23
- const match = ctx.schema.$src.path.match(/\/paths\/(?<path>.+)\/(?<method>.+)\/responses\/(?<status>\d+)\//);
24
- if (match && match.groups) {
25
- const { path, method, status } = match.groups;
26
- const endpoint = ctx.data.endpoints.find((e) => e.path === path && e.method === method);
27
- if (endpoint) {
28
- ctx.schema.name = `${endpoint.name}${status}Response`;
29
- }
30
- }
31
- }
32
14
  if (this.shouldGenerateTypeDeclaration(ctx, { schema: ctx.schema })) {
33
15
  const typeName = this.getDeclarationTypeName(ctx, { schema: ctx.schema });
34
16
  const packageName = this.getPackageName(ctx, { schema: ctx.schema });
@@ -184,6 +166,8 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
184
166
  return schema.enum && schema.enum.length > 0
185
167
  ? kt.call([this.getType(ctx, { schema }), toCasing(String(schema.default), ctx.config.enumValueNameCasing)])
186
168
  : kt.string(String(schema.default));
169
+ case 'array':
170
+ return kt.call(kt.refs.listOf.infer(), Array.isArray(schema.default) ? schema.default.map((x) => kt.toNode(x)) : []);
187
171
  default:
188
172
  return 'null';
189
173
  }
@@ -358,6 +342,9 @@ export class DefaultKotlinModelGenerator extends KotlinFileGenerator {
358
342
  if (schema.kind === 'object' && schema.properties.size === 0 && schema.additionalProperties) {
359
343
  return false;
360
344
  }
345
+ if (schema.kind === 'object' && ctx.config.emptyObjectTypeBehavior === 'use-any' && schema.properties.size === 0) {
346
+ return false;
347
+ }
361
348
  // Dynamically generated schemas do not have its own type declaration
362
349
  if (!ctx.data.schemas.some((x) => x.id === schema.id)) {
363
350
  return false;
@@ -4,6 +4,7 @@ export const defaultKotlinModelsGeneratorConfig = {
4
4
  packageName: 'com.openapi.generated',
5
5
  packageSuffix: '.model',
6
6
  oneOfBehavior: 'treat-as-any-of',
7
+ emptyObjectTypeBehavior: 'generate-empty-class',
7
8
  addJacksonAnnotations: true,
8
9
  addJakartaValidationAnnotations: true,
9
10
  addSwaggerAnnotations: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goast/kotlin",
3
- "version": "0.3.1",
3
+ "version": "0.3.3",
4
4
  "repository": "https://github.com/MaSch0212/goast.git",
5
5
  "description": "Provides gOAst generators for generating Kotlin code from OpenAPI specifications.",
6
6
  "keywords": [
@@ -23,9 +23,9 @@
23
23
  "type": "module",
24
24
  "dependencies": {
25
25
  "fs-extra": "^11.2.0",
26
- "node-fetch": "^3.3.2",
26
+ "node-fetch": "^2.7.0",
27
27
  "yaml": "^2.4.5",
28
- "@goast/core": "0.3.1",
28
+ "@goast/core": "0.3.3",
29
29
  "tslib": "2.6.3"
30
30
  },
31
31
  "packageManager": "pnpm@9.5.0",
@@ -2,11 +2,44 @@ import { ApiSchema, DefaultGenerationProviderConfig, OpenApiSchemasGenerationPro
2
2
  import { kt } from '../../ast';
3
3
  import { KotlinGeneratorConfig } from '../../config';
4
4
  export type KotlinModelsGeneratorConfig = KotlinGeneratorConfig & {
5
+ /**
6
+ * The package name for generated models.
7
+ * @default 'com.openapi.generated'
8
+ */
5
9
  packageName: string;
10
+ /**
11
+ * The suffix to append to the package name for generated models.
12
+ * @default '.model'
13
+ */
6
14
  packageSuffix: string | ((schema: ApiSchema) => string);
15
+ /**
16
+ * Determines how `oneOf` schemas should be treated.
17
+ * - `treat-as-any-of`: Treat `oneOf` schemas as `anyOf` schemas.
18
+ * - `treat-as-all-of`: Treat `oneOf` schemas as `allOf` schemas.
19
+ * @default 'treat-as-any-of'
20
+ */
7
21
  oneOfBehavior: 'treat-as-any-of' | 'treat-as-all-of';
22
+ /**
23
+ * Determines how schemas of type `object` without properties should be treated.
24
+ * - `use-any`: Generate `Any` type for empty object schemas.
25
+ * - `generate-empty-class`: Generate an empty class for empty object schemas.
26
+ * @default 'generate-empty-class'
27
+ */
28
+ emptyObjectTypeBehavior: 'use-any' | 'generate-empty-class';
29
+ /**
30
+ * Whether to add Jackson annotations to generated models.
31
+ * @default true
32
+ */
8
33
  addJacksonAnnotations: boolean;
34
+ /**
35
+ * Whether to add Jakarta Validation annotations to generated models.
36
+ * @default true
37
+ */
9
38
  addJakartaValidationAnnotations: boolean;
39
+ /**
40
+ * Whether to add Swagger annotations to generated models.
41
+ * @default true
42
+ */
10
43
  addSwaggerAnnotations: boolean;
11
44
  };
12
45
  export declare const defaultKotlinModelsGeneratorConfig: DefaultGenerationProviderConfig<KotlinModelsGeneratorConfig>;