Skip to content

Commit

Permalink
fix nullables with type (#2641)
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim authored Jan 2, 2024
1 parent 5b35001 commit 27d4c55
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "openapi-workspaces",
"license": "MIT",
"private": true,
"version": "0.53.17",
"version": "0.53.18",
"workspaces": [
"projects/json-pointer-helpers",
"projects/openapi-io",
Expand Down
2 changes: 1 addition & 1 deletion projects/fastify-capture/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/fastify-capture",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.17",
"version": "0.53.18",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/json-pointer-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/json-pointer-helpers",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.17",
"version": "0.53.18",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/openapi-io/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/openapi-io",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.17",
"version": "0.53.18",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/openapi-utilities/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/openapi-utilities",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.17",
"version": "0.53.18",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/optic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/optic",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.17",
"version": "0.53.18",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,57 @@ exports[`generateEndpointSpecPatches OAS version 3.0.1 allOf with nullable 2`] =
}
`;

exports[`generateEndpointSpecPatches OAS version 3.0.1 anyOf with nullable 1`] = `[]`;

exports[`generateEndpointSpecPatches OAS version 3.0.1 anyOf with nullable 2`] = `
{
"info": {},
"openapi": "3.0.1",
"paths": {
"/api/animals": {
"post": {
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"properties": {
"key": {
"anyOf": [
{
"properties": {
"goodbye": {
"type": "string",
},
"hello": {
"type": "string",
},
},
"required": [
"hello",
"goodbye",
],
"type": "object",
},
],
"nullable": true,
},
},
"required": [
"key",
],
"type": "object",
},
},
},
},
},
},
},
},
}
`;

exports[`generateEndpointSpecPatches OAS version 3.0.1 array with multiple items 1`] = `
[
{
Expand Down Expand Up @@ -3889,6 +3940,57 @@ exports[`generateEndpointSpecPatches OAS version 3.0.1 missing required property
}
`;

exports[`generateEndpointSpecPatches OAS version 3.0.1 oneOf with nullable 1`] = `[]`;

exports[`generateEndpointSpecPatches OAS version 3.0.1 oneOf with nullable 2`] = `
{
"info": {},
"openapi": "3.0.1",
"paths": {
"/api/animals": {
"post": {
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"properties": {
"key": {
"nullable": true,
"oneOf": [
{
"properties": {
"goodbye": {
"type": "string",
},
"hello": {
"type": "string",
},
},
"required": [
"hello",
"goodbye",
],
"type": "object",
},
],
},
},
"required": [
"key",
],
"type": "object",
},
},
},
},
},
},
},
},
}
`;

exports[`generateEndpointSpecPatches OAS version 3.0.1 query parameter not documented 1`] = `
[
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -980,8 +980,11 @@ describe('generateEndpointSpecPatches', () => {
expect(patches).toMatchSnapshot();
expect(specHolder.spec).toMatchSnapshot();
});
});

test('with nullable', async () => {
test.each([['oneOf'], ['anyOf'], ['allOf']])(
'%s with nullable',
async (polymorphic) => {
// nullable keyword only valid in 3.0.1
if (version !== '3.0.1') return;
specHolder.spec.paths['/api/animals'].post.responses = {
Expand All @@ -994,7 +997,7 @@ describe('generateEndpointSpecPatches', () => {
properties: {
key: {
nullable: true,
allOf: [
[polymorphic]: [
{
type: 'object',
properties: {
Expand Down Expand Up @@ -1038,8 +1041,8 @@ describe('generateEndpointSpecPatches', () => {

expect(patches).toMatchSnapshot();
expect(specHolder.spec).toMatchSnapshot();
});
});
}
);

describe.each([['query'], ['header']])('%s parameter', (location) => {
test('not documented', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,20 @@ function prepareSchemaForDiff(input: SchemaObject): SchemaObject {

// Fix case where nullable is set and there is no type key
if (!schema.type && schema.nullable) {
// if polymorphic, this could also be nullable
if (!(schema.oneOf || schema.anyOf || schema.allOf)) {
if (schema.oneOf || schema.anyOf || schema.allOf) {
if (schema.oneOf || schema.anyOf) {
(schema.oneOf || schema.anyOf).push({
type: 'object',
nullable: true,
});
} else {
schema.allOf = schema.allOf.map((s: any) => ({
...s,
nullable: true,
}));
}
delete schema.nullable;
} else {
// We set this to string since we're not sure what the actual type is; this should be fine for us since we'll use this schema for diffing purposes and update
schema.type = 'string';
}
Expand Down
2 changes: 1 addition & 1 deletion projects/rulesets-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/rulesets-base",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.17",
"version": "0.53.18",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion projects/standard-rulesets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@useoptic/standard-rulesets",
"license": "MIT",
"packageManager": "[email protected]",
"version": "0.53.17",
"version": "0.53.18",
"main": "build/index.js",
"types": "build/index.d.ts",
"files": [
Expand Down

0 comments on commit 27d4c55

Please sign in to comment.