Skip to content

Fix/add class-validator and swagger decorator into combined model class#25

Closed
JackCme wants to merge 2 commits into
omar-dulaimi:masterfrom
JackCme:fix/combined-class-decorators
Closed

Fix/add class-validator and swagger decorator into combined model class#25
JackCme wants to merge 2 commits into
omar-dulaimi:masterfrom
JackCme:fix/combined-class-decorators

Conversation

@JackCme

@JackCme JackCme commented Sep 16, 2025

Copy link
Copy Markdown

When using separateRelationFields=true with swagger=true, the combined class was missing class-validator and Swagger decorators for relation fields.

Changes:

  • Add class-validator imports (IsDefined, IsOptional) for relation fields
  • Add Swagger imports (ApiProperty) when swagger option is enabled
  • Add decorators to relation field properties in combined class
  • Add comprehensive tests to verify decorator generation

This ensures the combined class properly decorates relation fields, matching the behavior of RelationClass and single class generation modes.

Summary by CodeRabbit

  • New Features

    • Generated models now apply class-validator and Swagger decorators to relation fields in combined classes.
    • Imports for relation types, validators, and Swagger are generated only when needed, avoiding empty imports.
    • With separateRelationFields enabled, combined models extend their Base classes and include properly decorated relation properties.
  • Tests

    • Added comprehensive tests verifying decorator application, correct inheritance, import presence, and separation of base vs. relation models using a feature-rich schema.

…eRelationFields

When using separateRelationFields=true with swagger=true, the combined class
was missing class-validator and Swagger decorators for relation fields.

Changes:
- Add class-validator imports (IsDefined, IsOptional) for relation fields
- Add Swagger imports (ApiProperty) when swagger option is enabled
- Add decorators to relation field properties in combined class
- Add comprehensive tests to verify decorator generation

This ensures the combined class properly decorates relation fields, matching
the behavior of RelationClass and single class generation modes.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 16, 2025

Copy link
Copy Markdown

Walkthrough

The generator now conditionally emits relation imports, adds validator and Swagger imports for relation fields in combined classes, and applies decorators to relation properties. Tests were added to verify generated base, relation, and combined classes, including imports, inheritance, relation field decorators, and separation of scalar vs. relation fields.

Changes

Cohort / File(s) Summary
Generator: conditional imports & decorators for relations
src/generate-class.ts
- Only generate relation imports when present
- In combined class generation: compute and emit validator imports for relation fields; compute and emit Swagger imports when enabled and required; prepend field decorators to relation properties; preserve relation-type import generation logic
Tests: swagger + separateRelationFields coverage
tests/swagger-generation.test.ts
- New suite validates combined/base/relation outputs from schemas/full-features.prisma
- Asserts imports (Base, class-validator, Swagger), inheritance, relation field decorators, and separation of scalar vs. relation fields across User, Post, UserBase, UserRelations

Sequence Diagram(s)

sequenceDiagram
  actor Dev as Developer
  participant Gen as generateCombinedClass
  participant Rel as Relation Fields
  participant Imp as Import Generators
  participant Dec as Decorator Resolver
  participant Out as Emitter

  Dev->>Gen: Invoke for model
  Gen->>Rel: Collect relation fields
  alt any relations
    Gen->>Dec: getDecoratorsByFieldType(field, config.swagger)
    Dec-->>Gen: Decorator list per relation field
    Gen->>Imp: compute validatorImports from relation fields
    Imp-->>Gen: validatorImports (maybe empty)
    Gen->>Imp: compute swaggerImports if swagger enabled and needed
    Imp-->>Gen: swaggerImports (maybe empty)
    Gen->>Out: Emit validatorImports if non-empty
    Gen->>Out: Emit swaggerImports if non-empty
    Gen->>Out: Emit relation-type imports if needed
    Gen->>Out: Emit class with decorated relation properties
  else no relations
    Gen->>Out: Emit class without relation imports/decorators
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

I stitched new threads where relations bloom,
Imports peek in—only when there’s room.
Validators hop, Swagger waves a paw,
Fields wear decals, tidy to the law.
Base and Relations dance in pairs—
A rabbit nods: “All tests? It fares!” 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title accurately and concisely describes the primary change — adding and fixing class-validator and Swagger decorators on the combined model class — and aligns with the PR objectives and file-level changes. It is specific enough for a reviewer to understand the main intent without listing implementation details. The wording "Fix/add" is slightly informal but does not obscure the purpose.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Please see the documentation for more information.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
      - name: "Undocumented Breaking Changes"
        mode: "warning"
        instructions: |
          Pass/fail criteria: All breaking changes to public APIs, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints must be documented in the "Breaking Change" section of the PR description and in CHANGELOG.md. Exclude purely internal or private changes (e.g., code not exported from package entry points or explicitly marked as internal).

Please share your feedback with us on this Discord post.


Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (6)
src/generate-class.ts (3)

224-228: Guarded relation imports: good; also fix self-relations and mirror this guard in single-class.

  • Nice: avoids emitting import { } from "./".
  • Edge case: in Relations files, self-relations (e.g., UserRelations field typed User) currently skip importing User, causing an unresolved type in that file. Importing the model type is safe here and required.

Apply in Relations file (safe for self-relations):

-  relationFields.forEach((field) => {
-    if (field.relationName && model.name !== field.type) {
-      relationImports.add(field.type);
-    }
-  });
+  relationFields.forEach((field) => {
+    if (field.relationName) {
+      relationImports.add(field.type); // include self type (e.g., "User") for Relations file
+    }
+  });

Mirror the same empty-import guard in generateSingleClass to keep behavior consistent:

-  generateRelationImportsImport(sourceFile, [
-    ...relationImports,
-  ] as Array<string>);
+  if (relationImports.size > 0) {
+    generateRelationImportsImport(sourceFile, [
+      ...relationImports,
+    ] as Array<string>);
+  }

269-276: Deterministic import order for relation validator imports.

Sort to reduce diff churn across runs.

-  const validatorImports = [
-    ...new Set(
-      relationFields
-        .map((field) => getDecoratorsImportsByType(field))
-        .flatMap((item) => item),
-    ),
-  ];
+  const validatorImports = Array.from(
+    new Set(relationFields.flatMap((f) => getDecoratorsImportsByType(f))),
+  ).sort();

Also applies to: 278-281


283-292: Swagger import condition is correct; drop redundant length check.

shouldImportSwagger(relationFields) already implies non-empty.

-  if (
-    config.swagger &&
-    relationFields.length > 0 &&
-    shouldImportSwagger(relationFields as PrismaDMMF.Field[])
-  ) {
+  if (config.swagger && shouldImportSwagger(relationFields as PrismaDMMF.Field[])) {
tests/swagger-generation.test.ts (3)

74-83: Avoid rebuilding twice and run sequentially to reduce flakiness.

Build once or run suites sequentially; remove duplicate npm run build.

-describe('Combined Class with separateRelationFields', () => {
+describe.sequential('Combined Class with separateRelationFields', () => {
   beforeAll(async () => {
-    // Build the generator first
-    await execAsync('npm run build');
-
     // Generate models for full-features schema (has both swagger and separateRelationFields)
     const schemaPath = path.resolve(__dirname, 'schemas/full-features.prisma');
     await execAsync(`npx prisma generate --schema="${schemaPath}"`);
   }, 60000);

95-101: Make import assertions resilient to quote/style changes.

Use regex to tolerate single/double quotes and spacing.

-    // Check for Swagger imports
-    expect(userModel).toContain('import { ApiProperty } from "@nestjs/swagger"');
+    // Check for Swagger imports
+    expect(userModel).toMatch(/import\s+\{\s*ApiProperty\s*\}\s+from\s+["']@nestjs\/swagger["']/);

127-133: Same resilience for class-validator/Swagger imports in Post model.

-    // Check for class-validator imports for optional relation
-    expect(postModel).toContain('import { IsOptional } from "class-validator"');
+    // Check for class-validator imports for optional relation
+    expect(postModel).toMatch(/import\s+\{\s*IsOptional\s*\}\s+from\s+["']class-validator["']/);
 
-    // Check for Swagger imports
-    expect(postModel).toContain('import { ApiProperty } from "@nestjs/swagger"');
+    // Check for Swagger imports
+    expect(postModel).toMatch(/import\s+\{\s*ApiProperty\s*\}\s+from\s+["']@nestjs\/swagger["']/);
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b87a3bc and 6e45a82.

📒 Files selected for processing (2)
  • src/generate-class.ts (3 hunks)
  • tests/swagger-generation.test.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/generate-class.ts (1)
src/helpers.ts (7)
  • generateRelationImportsImport (260-268)
  • getDecoratorsImportsByType (213-241)
  • generateClassValidatorImport (243-251)
  • shouldImportSwagger (295-297)
  • getSwaggerImportsByType (299-303)
  • generateSwaggerImport (305-313)
  • getDecoratorsByFieldType (80-145)
🔇 Additional comments (1)
src/generate-class.ts (1)

323-323: Decorators now applied to combined relation fields.

Fix addresses the missing validators/Swagger on relations in combined classes.

@JackCme JackCme closed this Sep 17, 2025
@JackCme
JackCme deleted the fix/combined-class-decorators branch September 17, 2025 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant