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

fix(schema): fix integer type when Nullable decorator is used #2756

Merged
merged 1 commit into from
Jul 18, 2024
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {inlineEnums} from "./inlineEnums.js";
import {inlineEnumsMapper} from "./inlineEnumsMapper";

describe("inlineEnums()", () => {
describe("inlineEnumsMapper()", () => {
it("should inline enums", () => {
const result = inlineEnums(
const result = inlineEnumsMapper(
{
enum: {
$isJsonDocument: true,
Expand All @@ -22,7 +22,7 @@ describe("inlineEnums()", () => {
});

it("should inline enums and set type (object to string)", () => {
const result = inlineEnums(
const result = inlineEnumsMapper(
{
type: "object",
enum: {
Expand All @@ -42,7 +42,7 @@ describe("inlineEnums()", () => {
});
});
it("should inline enums and keep the type", () => {
const result = inlineEnums(
const result = inlineEnumsMapper(
{
type: "string",
enum: {
Expand Down
17 changes: 17 additions & 0 deletions packages/specs/schema/src/components/default/inlineEnumsMapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {JsonSchema} from "../../domain/JsonSchema";
import {JsonSchemaOptions} from "../../interfaces/JsonSchemaOptions";
import {registerJsonSchemaMapper} from "../../registries/JsonSchemaMapperContainer";

export function inlineEnumsMapper(obj: any, schema: JsonSchema, options: JsonSchemaOptions) {
if (options.inlineEnums && obj.enum?.$isJsonDocument) {
obj.enum = obj.enum.toJSON().enum;
}

if (obj.enum) {
obj.type = obj.type === "object" || obj.type === undefined ? "string" : obj.type;
}

return obj;
}

registerJsonSchemaMapper("inlineEnums", inlineEnumsMapper);
54 changes: 54 additions & 0 deletions packages/specs/schema/src/components/default/nullableMapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {cleanObject} from "@tsed/core";
import {MANY_OF_PROPERTIES} from "../../constants/jsonSchemaProperties";
import type {JsonSchema} from "../../domain/JsonSchema";
import {registerJsonSchemaMapper} from "../../registries/JsonSchemaMapperContainer";

export function nullableMapper(obj: any, schema: JsonSchema | null) {
if (!schema?.isNullable) {
return obj;
}

if (!obj.discriminator) {
if (obj.$ref) {
obj = cleanObject({
...obj,
$ref: undefined,
anyOf: [
{type: "null"},
{
$ref: obj.$ref
}
]
});
} else {
MANY_OF_PROPERTIES.some((keyword) => {
if (obj[keyword]) {
obj[keyword] = obj[keyword].filter((item: any) => item.type !== "null");

if (obj[keyword].length === 1) {
const base = obj[keyword];

obj = cleanObject({
...obj,
...base[0],
[keyword]: undefined,
type: ["null", Number.isInteger(obj.multipleOf) ? "integer" : base[0].type]
});
} else {
obj[keyword] = [{type: "null"}].concat(obj[keyword]).map((item: any) => {
if (Number.isInteger(item.multipleOf)) {
item.type = "integer";
}
return item;
});
delete obj.type;
}
}
});
}
}

return obj;
}

registerJsonSchemaMapper("nullable", nullableMapper);
3 changes: 1 addition & 2 deletions packages/specs/schema/src/components/default/objectMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {JsonSchema} from "../../domain/JsonSchema.js";
import {alterIgnore} from "../../hooks/alterIgnore.js";
import {JsonSchemaOptions} from "../../interfaces/JsonSchemaOptions.js";
import {execMapper, registerJsonSchemaMapper} from "../../registries/JsonSchemaMapperContainer.js";
import {mapNullableType} from "../../utils/mapNullableType.js";

/**
* Serialize Any object to a json schema
Expand All @@ -24,7 +23,7 @@ export function objectMapper(input: any, options: JsonSchemaOptions) {

// remove groups to avoid bad schema generation over children models
obj[key] = execMapper("item", [value], opts);
obj[key] = mapNullableType(obj[key], value, opts);
obj[key] = execMapper("nullable", [obj[key], value], opts);
}

return obj;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {uniq} from "@tsed/core";
import type {JsonSchema} from "../domain/JsonSchema.js";
import {alterRequiredGroups} from "../hooks/alterRequiredGroups.js";
import type {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions.js";
import type {JsonSchema} from "../../domain/JsonSchema";
import {alterRequiredGroups} from "../../hooks/alterRequiredGroups";
import type {JsonSchemaOptions} from "../../interfaces/JsonSchemaOptions";
import {registerJsonSchemaMapper} from "../../registries/JsonSchemaMapperContainer";

function mapRequiredProps(obj: any, schema: JsonSchema, options: JsonSchemaOptions = {}) {
const {useAlias} = options;
Expand Down Expand Up @@ -36,10 +37,7 @@ function extractRequiredProps(obj: any, schema: JsonSchema, options: JsonSchemaO
return alterRequiredGroups(uniq(required), schema, options);
}

/**
* @ignore
*/
export function getRequiredProperties(obj: any, schema: JsonSchema, options: JsonSchemaOptions) {
export function requiredMapper(obj: any, schema: JsonSchema, options: JsonSchemaOptions) {
if (options.groups && options.groups.includes("partial")) {
if (obj.discriminator) {
return {
Expand Down Expand Up @@ -68,3 +66,5 @@ export function getRequiredProperties(obj: any, schema: JsonSchema, options: Jso

return obj;
}

registerJsonSchemaMapper("required", requiredMapper);
9 changes: 3 additions & 6 deletions packages/specs/schema/src/components/default/schemaMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import {SpecTypes} from "../../domain/SpecTypes.js";
import {alterOneOf} from "../../hooks/alterOneOf.js";
import {JsonSchemaOptions} from "../../interfaces/JsonSchemaOptions.js";
import {execMapper, hasMapper, registerJsonSchemaMapper} from "../../registries/JsonSchemaMapperContainer.js";
import {getRequiredProperties} from "../../utils/getRequiredProperties.js";
import {inlineEnums} from "../../utils/inlineEnums.js";
import {mapNullableType} from "../../utils/mapNullableType.js";

/**
* @ignore
Expand Down Expand Up @@ -132,10 +129,10 @@ function serializeSchema(schema: JsonSchema, options: JsonSchemaOptions) {
};
}

obj = getRequiredProperties(obj, schema, options);
obj = mapNullableType(obj, schema, options);
obj = execMapper("required", [obj, schema], options);
obj = execMapper("nullable", [obj, schema], options);
obj = alterOneOf(obj, schema, options);
obj = inlineEnums(obj, schema, options);
obj = execMapper("inlineEnums", [obj, schema], options);

return obj;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/specs/schema/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ export * from "./default/anyMapper.js";
export * from "./default/classMapper.js";
export * from "./default/genericsMapper.js";
export * from "./default/inheritedClassMapper.js";
export * from "./default/inlineEnumsMapper.js";
export * from "./default/itemMapper.js";
export * from "./default/lazyRefMapper.js";
export * from "./default/mapMapper.js";
export * from "./default/nullableMapper.js";
export * from "./default/objectMapper.js";
export * from "./default/ofMapper.js";
export * from "./default/propertiesMapper.js";
export * from "./default/requiredMapper.js";
export * from "./default/schemaMapper.js";
export * from "./open-spec/generate.js";
export * from "./open-spec/nullableMapper.js";
export * from "./open-spec/operationInFilesMapper.js";
export * from "./open-spec/operationInParameterMapper.js";
export * from "./open-spec/operationInParametersMapper.js";
Expand Down
50 changes: 50 additions & 0 deletions packages/specs/schema/src/components/open-spec/nullableMapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {cleanObject} from "@tsed/core";
import type {JsonSchema} from "../../domain/JsonSchema";
import {SpecTypes} from "../../domain/SpecTypes";
import {registerJsonSchemaMapper} from "../../registries/JsonSchemaMapperContainer";

export function nullableMapperOpenApi(obj: any, schema: JsonSchema | null) {
if (!schema?.isNullable) {
return obj;
}

if (obj.$ref) {
return cleanObject({
...obj,
...(obj.$ref && {
anyOf: [
{
$ref: obj.$ref
}
],
type: undefined
}),
nullable: true,
$ref: undefined
});
}

return cleanObject({
...obj,
...(obj.anyOf?.length === 1
? {
...obj.anyOf[0],
anyOf: undefined,
type: Number.isInteger(obj.multipleOf) ? "integer" : obj.anyOf[0]?.type
}
: {
type: obj.anyOf?.length > 1 ? undefined : obj.type,
anyOf: obj.anyOf?.map((item: any) => {
if (Number.isInteger(item.multipleOf)) {
item.type = "integer";
}

return item;
})
}),
nullable: true
});
}

registerJsonSchemaMapper("nullable", nullableMapperOpenApi, SpecTypes.OPENAPI);
registerJsonSchemaMapper("nullable", nullableMapperOpenApi, SpecTypes.SWAGGER);
1 change: 1 addition & 0 deletions packages/specs/schema/src/decorators/common/integer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type {JSONSchema6} from "json-schema";
import {JsonEntityFn} from "./jsonEntityFn.js";

/**
Expand Down
114 changes: 113 additions & 1 deletion packages/specs/schema/src/decorators/common/nullable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {BodyParams} from "@tsed/platform-params";
import Ajv from "ajv";
import {type} from "node:os";
import {SpecTypes} from "../../domain/SpecTypes.js";
import {getJsonSchema} from "../../utils/getJsonSchema.js";
import {getSpec} from "../../utils/getSpec.js";
import {In} from "../operations/in.js";
import {Path} from "../operations/path.js";
import {Post} from "../operations/route.js";
import {Format} from "./format.js";
import {Integer} from "./integer";
import {MaxLength} from "./maxLength.js";
import {Minimum} from "./minimum.js";
import {Nullable} from "./nullable.js";
Expand Down Expand Up @@ -176,6 +176,118 @@ describe("@Nullable", () => {
type: "object"
});
});
it("should declare any prop (Integer + Nullable)", () => {
// WHEN
class Model {
@Integer()
prop1: number;

@Nullable(Number)
@Integer()
prop2: number | null;

@Nullable(Number, String)
@Integer()
prop3: number | string | null;
}

@Path("/")
class MyController {
@Post("/")
body(@BodyParams() model: Model) {}
}

// THEN
expect(getJsonSchema(Model)).toEqual({
properties: {
prop1: {
multipleOf: 1,
type: "integer"
},
prop2: {
multipleOf: 1,
type: ["null", "integer"]
},
prop3: {
anyOf: [
{
type: "null"
},
{
multipleOf: 1,
type: "integer"
},
{
type: "string"
}
]
}
},
type: "object"
});

expect(getSpec(MyController, {specType: SpecTypes.OPENAPI})).toEqual({
components: {
schemas: {
Model: {
properties: {
prop1: {
multipleOf: 1,
type: "integer"
},
prop2: {
multipleOf: 1,
nullable: true,
type: "integer"
},
prop3: {
anyOf: [
{
multipleOf: 1,
type: "integer"
},
{
type: "string"
}
],
nullable: true
}
},
type: "object"
}
}
},
paths: {
"/": {
post: {
operationId: "myControllerBody",
parameters: [],
requestBody: {
content: {
"application/json": {
schema: {
$ref: "#/components/schemas/Model"
}
}
},
required: false
},
responses: {
"200": {
description: "Success"
}
},
tags: ["MyController"]
}
}
},
tags: [
{
name: "MyController"
}
]
});
});
it("should declare any prop (String & Number + Nullable)", () => {
// WHEN
class Model {
Expand Down
2 changes: 0 additions & 2 deletions packages/specs/schema/src/domain/JsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {IgnoreCallback} from "../interfaces/IgnoreCallback.js";
import {JsonSchemaOptions} from "../interfaces/JsonSchemaOptions.js";
import {enumsRegistry} from "../registries/enumRegistries.js";
import {execMapper} from "../registries/JsonSchemaMapperContainer.js";
import {string} from "../utils/from.js";
import {NestedGenerics} from "../utils/generics.js";
import {getComputedType} from "../utils/getComputedType.js";
import {getJsonType} from "../utils/getJsonType.js";
Expand Down Expand Up @@ -824,7 +823,6 @@ export class JsonSchema extends Map<string, any> implements NestedGenerics {
break;

case "integer":
super.set("type", getJsonType(type));
this.integer();
break;

Expand Down
Loading
Loading