Skip to content

Commit

Permalink
fix(openapi): support nullish consts (#751)
Browse files Browse the repository at this point in the history
  • Loading branch information
WoH authored Sep 25, 2023
1 parent e40e96d commit ff02608
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/spec/openapi/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function transformDefsToComponents (jsonSchema) {
if (jsonSchema.patternProperties) {
jsonSchema.additionalProperties = Object.values(jsonSchema.patternProperties)[0]
delete jsonSchema.patternProperties
} else if (jsonSchema.const) {
} else if (jsonSchema.const !== undefined) {
// OAS 3.1 supports `const` but it is not supported by `swagger-ui`
// https://swagger.io/docs/specification/data-models/keywords/
jsonSchema.enum = [jsonSchema.const]
Expand Down
2 changes: 1 addition & 1 deletion lib/spec/swagger/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function replaceUnsupported (jsonSchema) {
if (jsonSchema.patternProperties) {
jsonSchema.additionalProperties = { type: 'string' }
delete jsonSchema.patternProperties
} else if (jsonSchema.const) {
} else if (jsonSchema.const !== undefined) {
// Handle const, that is not part of OpenAPI definitions
jsonSchema.enum = [jsonSchema.const]
delete jsonSchema.const
Expand Down
18 changes: 17 additions & 1 deletion test/spec/openapi/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,11 @@ test('support "const" keyword', async t => {
obj: {
type: 'object',
properties: {
constantProp: { const: 'my-const' }
constantProp: { const: 'my-const' },
constantPropZero: { const: 0 },
constantPropNull: { const: null },
constantPropFalse: { const: false },
constantPropEmptyString: { const: '' }
}
}
}
Expand Down Expand Up @@ -685,6 +689,18 @@ test('support "const" keyword', async t => {
properties: {
constantProp: {
enum: ['my-const']
},
constantPropZero: {
enum: [0]
},
constantPropNull: {
enum: [null]
},
constantPropFalse: {
enum: [false]
},
constantPropEmptyString: {
enum: ['']
}
}
}
Expand Down
18 changes: 17 additions & 1 deletion test/spec/swagger/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,11 @@ test('support "const" keyword', async t => {
obj: {
type: 'object',
properties: {
constantProp: { const: 'my-const' }
constantProp: { const: 'my-const' },
constantPropNull: { const: null },
constantPropZero: { const: 0 },
constantPropFalse: { const: false },
constantPropEmptyString: { const: '' }
}
}
}
Expand All @@ -475,6 +479,18 @@ test('support "const" keyword', async t => {
properties: {
constantProp: {
enum: ['my-const']
},
constantPropZero: {
enum: [0]
},
constantPropNull: {
enum: [null]
},
constantPropFalse: {
enum: [false]
},
constantPropEmptyString: {
enum: ['']
}
}
}
Expand Down

0 comments on commit ff02608

Please sign in to comment.