Skip to content

fix: change parsing of type: null to add nullable: true to oneOf/anyOf #43

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

Merged
merged 1 commit into from
Mar 28, 2023
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
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^9.0.9",
"json-schema-walker": "^1.1.0",
"openapi-types": "^12.0.2",
"openapi-types": "^12.1.0",
"yargs": "^17.6.2"
},
"devDependencies": {
"@types/json-schema": "^7.0.11",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@typescript-eslint/parser": "^5.42.0",
"@typescript-eslint/eslint-plugin": "^5.49.0",
"@typescript-eslint/parser": "^5.49.0",
"c8": "^7.12.0",
"eslint": "^8.27.0",
"eslint-config-prettier": "^8.5.0",
"eslint": "^8.32.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-unused-imports": "^2.0.0",
"nock": "^13.2.9",
"prettier": "^2.7.1",
"typescript": "^4.8.4",
"vitest": "^0.24.5"
"nock": "^13.3.0",
"prettier": "^2.8.3",
"typescript": "^4.9.4",
"vitest": "^0.28.1"
},
"prettier": {
"singleQuote": true,
Expand Down
36 changes: 31 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class InvalidTypeError extends Error {

const oasExtensionPrefix = 'x-';

const handleDefinition = async <T extends JSONSchema = JSONSchema>(
const handleDefinition = async <T extends JSONSchema4 = JSONSchema4>(
def: JSONSchema7Definition | JSONSchema6Definition | JSONSchema4,
schema: T
) => {
Expand Down Expand Up @@ -52,7 +52,8 @@ const handleDefinition = async <T extends JSONSchema = JSONSchema>(
delete (<any>walker.rootSchema).definitions;
}
return walker.rootSchema;
} else if (Array.isArray(def)) {
}
if (Array.isArray(def)) {
// if it's an array, we might want to reconstruct the type;
const typeArr = def;
const hasNull = typeArr.includes('null');
Expand All @@ -69,7 +70,7 @@ const handleDefinition = async <T extends JSONSchema = JSONSchema>(
return def;
};

const convert = async <T extends JSONSchema = JSONSchema>(
const convert = async <T extends object = JSONSchema4>(
schema: T,
options?: Options
): Promise<OpenAPIV3.Document> => {
Expand Down Expand Up @@ -108,6 +109,7 @@ function convertSchema(schema: SchemaType | undefined) {
schema = convertTypes(schema);
schema = rewriteConst(schema);
schema = convertDependencies(schema);
schema = convertNullable(schema);
schema = rewriteIfThenElse(schema);
schema = rewriteExclusiveMinMax(schema);
schema = convertExamples(schema);
Expand Down Expand Up @@ -196,6 +198,30 @@ function convertDependencies(schema: SchemaType) {
return schema;
}

function convertNullable(schema: SchemaType) {
for (const key of ['oneOf', 'anyOf'] as const) {
const schemas = schema[key] as JSONSchema4[];
if (!Array.isArray(schemas)) {
return schema;
}

const hasNullable = schemas.some((item) => item.type === 'null');

if (!hasNullable) {
return schema;
}

const filtered = schemas.filter((l) => l.type !== 'null');
for (const schemaEntry of filtered) {
schemaEntry.nullable = true;
}

schema[key] = filtered;
}

return schema;
}

function convertTypes(schema: SchemaType) {
if (typeof schema !== 'object') {
return schema;
Expand Down Expand Up @@ -300,11 +326,11 @@ function rewriteIfThenElse(schema: SchemaType) {
function rewriteExclusiveMinMax(schema: SchemaType) {
if (typeof schema.exclusiveMaximum === 'number') {
schema.maximum = schema.exclusiveMaximum;
schema.exclusiveMaximum = true;
(schema as JSONSchema4).exclusiveMaximum = true;
}
if (typeof schema.exclusiveMinimum === 'number') {
schema.minimum = schema.exclusiveMinimum;
schema.exclusiveMinimum = true;
(schema as JSONSchema4).exclusiveMinimum = true;
}
return schema;
}
Expand Down
10 changes: 5 additions & 5 deletions test/default-null.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ it('supports default values of null', async ({ expect }) => {
nullableStringWithDefault: {
default: null,
oneOf: [{ type: 'string' }, { type: 'null' }],
}
}
},
},
};

const result = await convert(schema);
Expand All @@ -19,9 +19,9 @@ it('supports default values of null', async ({ expect }) => {
properties: {
nullableStringWithDefault: {
default: null,
oneOf: [{ type: 'string' }, { nullable: true }],
}
}
oneOf: [{ type: 'string', nullable: true }],
},
},
};

expect(result).toEqual(expected);
Expand Down
Loading