Skip to content

Commit

Permalink
refactor(sample): use openapi cli plugin for dtos and schema (#1611)
Browse files Browse the repository at this point in the history
## Description
Use openapi plugin in samples module

## Motivation
<!-- Background on use case, changes needed -->

## Fixes
Related to #1590  

## Changes:
* Removed all @ApiProperty decorators in samples module.

## Tests included

- [ ] Included for each change/fix?
- [x] Passing? <!-- Merge will not be approved unless tests pass -->

## Documentation
- [ ] swagger documentation updated (required for API changes)
- [ ] official documentation updated
  • Loading branch information
emigun authored Jan 22, 2025
2 parents f463abb + 2bbf31a commit d33e8a8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 77 deletions.
8 changes: 7 additions & 1 deletion nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"plugins": ["@nestjs/swagger"]
"plugins": [{
"name": "@nestjs/swagger",
"options": {
"dtoFileNameSuffix": [".dto.ts", "sample.schema.ts"],
"introspectComments": true
}
}]
}
}
10 changes: 3 additions & 7 deletions src/samples/dto/create-sample.dto.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsOptional, IsString } from "class-validator";
import { UpdateSampleDto } from "./update-sample.dto";

export class CreateSampleDto extends UpdateSampleDto {
@ApiProperty({
type: String,
required: false,
description:
"Globally unique identifier of a sample. This could be provided as an input value or generated by the system.",
})
/**
* Globally unique identifier of a sample. This could be provided as an input value or generated by the system.
*/
@IsString()
@IsOptional()
readonly sampleId?: string;
Expand Down
40 changes: 15 additions & 25 deletions src/samples/dto/update-sample.dto.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
import { ApiProperty, PartialType } from "@nestjs/swagger";
import { PartialType } from "@nestjs/swagger";
import { IsBoolean, IsObject, IsOptional, IsString } from "class-validator";
import { OwnableDto } from "../../common/dto/ownable.dto";

export class UpdateSampleDto extends OwnableDto {
@ApiProperty({
type: String,
required: false,
description: "The owner of the sample.",
})
/**
* The owner of the sample.
*/
@IsString()
@IsOptional()
readonly owner?: string;

@ApiProperty({
type: String,
required: false,
description: "A description of the sample.",
})
/**
* A description of the sample.
*/
@IsString()
@IsOptional()
readonly description?: string;

@ApiProperty({
type: Object,
default: {},
required: false,
description: "JSON object containing the sample characteristics metadata.",
})
/**
* JSON object containing the sample characteristics metadata.
*/
@IsObject()
@IsOptional()
readonly sampleCharacteristics?: Record<string, unknown>;
readonly sampleCharacteristics?: Record<string, unknown> = {};

@ApiProperty({
type: Boolean,
default: false,
required: false,
description: "Flag is true when data are made publicly available.",
})
/**
* Flag is true when data are made publicly available.
*/
@IsBoolean()
@IsOptional()
readonly isPublished?: boolean;
readonly isPublished?: boolean = false;
}

export class PartialUpdateSampleDto extends PartialType(UpdateSampleDto) {}
1 change: 0 additions & 1 deletion src/samples/samples.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const mockSample: SampleClass = {

describe("SamplesService", () => {
let service: SamplesService;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let sampleModel: Model<SampleClass>;

beforeEach(async () => {
Expand Down
64 changes: 21 additions & 43 deletions src/samples/schemas/sample.schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ApiProperty } from "@nestjs/swagger";
import { ApiHideProperty } from "@nestjs/swagger";
import { Document } from "mongoose";
import { Attachment } from "src/attachments/schemas/attachment.schema";
import { OwnableClass } from "src/common/schemas/ownable.schema";
Expand All @@ -16,68 +16,46 @@ export type SampleDocument = SampleClass & Document;
timestamps: true,
})
export class SampleClass extends OwnableClass {
@ApiHideProperty()
@Prop({ type: String })
_id: string;

@ApiProperty({
type: String,
default: () => uuidv4(),
required: true,
description:
"Globally unique identifier of a sample. This could be provided as an input value or generated by the system.",
})
/**
* Globally unique identifier of a sample. This could be provided as an input value or generated by the system.
*/
@Prop({ type: String, unique: true, required: true, default: () => uuidv4() })
sampleId: string;

@ApiProperty({
type: String,
required: false,
description: "The owner of the sample.",
})
/**
* The owner of the sample.
*/
@Prop({ type: String, required: false })
owner?: string;

@ApiProperty({
type: String,
required: false,
description: "A description of the sample.",
})
/**
* A description of the sample.
*/
@Prop({ type: String, required: false })
description?: string;

@ApiProperty({
type: Object,
default: {},
required: false,
description: "JSON object containing the sample characteristics metadata.",
})
/**
* JSON object containing the sample characteristics metadata.
*/
@Prop({ type: Object, required: false, default: {} })
sampleCharacteristics?: Record<string, unknown>;
sampleCharacteristics?: Record<string, unknown> = {};
}

export class SampleWithAttachmentsAndDatasets extends SampleClass {
/*
@ApiProperty({ type: "array", items: { $ref: getSchemaPath(Attachment) } })
@Prop([AttachmentSchema])*/
/**
* Attachments that are related to this sample.
*/
// this property should not be present in the database model
@ApiProperty({
type: Attachment,
isArray: true,
required: false,
description: "Attachments that are related to this sample.",
})
attachments?: Attachment[];

/*
@ApiProperty({ type: "array", items: { $ref: getSchemaPath(Dataset) } })
@Prop([DatasetSchema])*/
/**
* Datasets that are related to this sample.
*/
// this property should not be present in the database model
@ApiProperty({
type: DatasetClass,
isArray: true,
required: false,
description: "Datasets that are related to this sample.",
})
datasets?: DatasetClass[];
}

Expand Down

0 comments on commit d33e8a8

Please sign in to comment.