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

feat: Add a way to declare enums in interfaces #117

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,18 @@ Automatically generates swagger schema-descriptions based on your models

Instead of using `param: any` you can now use custom interfaces `param: UserDetails`. The interfaces files need to be located at `app/Interfaces/`

#### Usage of enums

Adding the @enum() at the end of the property will allow the correct values to be added to the specification.

```ts
export interface ICustomer {
email: string;
name: string;
gender: string; // @enum(male,female,genderless)
}
```

## Extend Models

Add additional documentation to your Models properties.
Expand Down
22 changes: 16 additions & 6 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ export class CommentParser {

private parseRequestFormDataBody(rawLine: string) {
const line = rawLine.replace("@requestFormDataBody ", "");
let json = {}, required = [];
let json = {},
required = [];
const isJson = isJSONString(line);
if (!isJson) {
// try to get json from reference
Expand All @@ -284,7 +285,8 @@ export class CommentParser {
let props = [];
const ref = this.exampleGenerator.schemas[cleandRef];
const ks = [];
if (ref.required && Array.isArray(ref.required)) required.push(...ref.required)
if (ref.required && Array.isArray(ref.required))
required.push(...ref.required);
Object.entries(ref.properties).map(([key, value]) => {
if (typeof parsedRef[key] === "undefined") {
return;
Expand Down Expand Up @@ -314,9 +316,9 @@ export class CommentParser {
json = JSON.parse(line);
for (let key in json) {
if (json[key].required === "true") {
required.push(key)
required.push(key);
}
}
}
}
// No need to try/catch this JSON.parse as we already did that in the isJSONString function

Expand Down Expand Up @@ -924,7 +926,7 @@ export class ValidatorParser {
: this.exampleGenerator.exampleByType("number"),
...meta,
};
if(!p["isOptional"]) obj[p["fieldName"]]["required"] = true;
if (!p["isOptional"]) obj[p["fieldName"]]["required"] = true;
}
return obj;
}
Expand Down Expand Up @@ -980,8 +982,13 @@ export class InterfaceParser {
type = type.replace("[]", "");
isArray = true;
}
let prop: any = { type: type };
let meta = "";
if (type.includes("@enum")) {
// Extract the enum values from the line
meta = type.substring(type.indexOf("@enum"));
type = "string";
}
let prop: any = { type: type };
let en = getBetweenBrackets(meta, "enum");
let example = getBetweenBrackets(meta, "example");
let enums = [];
Expand Down Expand Up @@ -1017,6 +1024,9 @@ export class InterfaceParser {

prop[indicator] = type;
prop["example"] = example;
if (enums.length > 0) {
prop["enum"] = enums.map((e) => e.trim()); // Clean the enum by removing start and end spaces
}
prop["nullable"] = notRequired;
}
if (isArray) {
Expand Down