Skip to content

Commit

Permalink
feat(schema): support Examples on QueryParams model
Browse files Browse the repository at this point in the history
Closes: #2633
  • Loading branch information
Romakita committed Mar 22, 2024
1 parent 745273f commit a1dbcc8
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 1 deletion.
@@ -1,9 +1,37 @@
import {cleanObject} from "@tsed/core";
import {OS3Example} from "@tsed/openspec";
import {JsonSchemaOptions} from "../../interfaces/JsonSchemaOptions";
import {registerJsonSchemaMapper} from "../../registries/JsonSchemaMapperContainer";
import {createRefName} from "../../utils/ref";
import type {JsonParameterOptions} from "./operationInParameterMapper";

function buildExamples(property: string, examples?: Record<string, OS3Example>) {
if (!examples) {
return undefined;
}

let hasKey = false;

const newExamples = Object.entries(examples).reduce((acc, [key, {value, description, ...props}]) => {
if (value[property] === undefined) {
return acc;
}

hasKey = true;

return {
...acc,
[key]: {
...props,
value: value[property],
description
}
};
}, {});

return hasKey ? newExamples : undefined;
}

function inlineReference(parameter: any, {jsonParameter, ...options}: JsonSchemaOptions) {
const name = createRefName(jsonParameter.$schema.getName(), options);
const schema = options.components?.schemas?.[name];
Expand All @@ -21,7 +49,8 @@ function inlineReference(parameter: any, {jsonParameter, ...options}: JsonSchema
required: (schema?.required || []).includes(key),
description,
schema: prop,
style: prop.$ref ? "deepObject" : undefined
style: prop.$ref ? "deepObject" : undefined,
examples: buildExamples(key, parameter.examples)
})
];
}, []);
Expand Down
124 changes: 124 additions & 0 deletions packages/specs/schema/test/integrations/query-model-examples.spec.ts
@@ -0,0 +1,124 @@
import {
Default,
Examples,
GenericOf,
Generics,
getSpec,
In,
Maximum,
Minimum,
OperationPath,
Path,
Property,
SpecTypes
} from "../../src/index";
import {QueryParams} from "@tsed/platform-params";

class QueryParamModel {
@Property()
path: string;

@Property()
condition: string;

@Property()
value: string;
}

@Path("/query")
class QueryModelCtrl {
@OperationPath("GET", "/")
async get(
@QueryParams()
@Examples({
example1: {
description: "description1",
value: {
path: "path1",
condition: "condition1"
}
},
example2: {
description: "description1",
value: {
path: "path2",
condition: "condition2"
}
}
})
q: QueryParamModel
) {}
}

describe("Query Model example", () => {
it("should generate the spec for deep object", () => {
const spec = getSpec(QueryModelCtrl, {specType: SpecTypes.OPENAPI});

expect(spec).toEqual({
paths: {
"/query": {
get: {
operationId: "queryModelCtrlGet",
parameters: [
{
in: "query",
name: "path",
required: false,
examples: {
example1: {
description: "description1",
value: "path1"
},
example2: {
description: "description1",
value: "path2"
}
},
schema: {
type: "string"
}
},
{
examples: {
example1: {
description: "description1",
value: "condition1"
},
example2: {
description: "description1",
value: "condition2"
}
},
in: "query",
name: "condition",
required: false,
schema: {
type: "string"
}
},
{
in: "query",
name: "value",
required: false,
schema: {
type: "string"
}
}
],
responses: {
"200": {
description: "Success"
}
},
tags: ["QueryModelCtrl"]
}
}
},
tags: [
{
name: "QueryModelCtrl"
}
]
});
});
});

0 comments on commit a1dbcc8

Please sign in to comment.