Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(sample): use openapi cli plugin for dtos and schema #1611

Merged
merged 2 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading