Implement 'const' as synonym for single valued 'enum'; when type is defined, don't aggregate types from oneOf variants

This commit is contained in:
Stuart McGrigor 2019-05-23 15:47:18 +12:00
parent 18590ccc60
commit 99f845731b
3 changed files with 22 additions and 11 deletions

View File

@ -53,6 +53,9 @@ export class FieldDetails extends React.PureComponent<FieldProps> {
)}
<FieldDetail label={l('default') + ':'} value={schema.default} />
{!renderDiscriminatorSwitch && <EnumValues type={schema.type} values={schema.enum} />}{' '}
{schema.const && <FieldDetail label={l('enumSingleValue') + ':'} value={schema.const} />}
{showExamples && <FieldDetail label={l('example') + ':'} value={example} />}
{<Extensions extensions={{ ...field.extensions, ...schema.extensions }} />}
<div>

View File

@ -20,6 +20,7 @@ import {
} from '../../utils/';
import { l } from '../Labels';
import { Schema } from '../../components';
// TODO: refactor this model, maybe use getters instead of copying all the values
export class SchemaModel {
@ -42,6 +43,7 @@ export class SchemaModel {
pattern?: string;
example?: any;
enum: any[];
const: any;
default?: any;
readOnly: boolean;
writeOnly: boolean;
@ -105,6 +107,7 @@ export class SchemaModel {
this.format = schema.format;
this.nullable = !!schema.nullable;
this.enum = schema.enum || [];
this.const = schema.const;
this.example = schema.example;
this.deprecated = !!schema.deprecated;
this.pattern = schema.pattern;
@ -195,17 +198,21 @@ export class SchemaModel {
return schema;
});
this.displayType = this.oneOf
.map(schema => {
let name =
schema.typePrefix +
(schema.title ? `${schema.title} (${schema.displayType})` : schema.displayType);
if (name.indexOf(' or ') > -1) {
name = `(${name})`;
}
return name;
})
.join(' or ');
// If the displayType hasn't been defined, then assemble an aggregate
// displayType from all the oneOf variants
if (this.displayType == 'any') {
this.displayType = this.oneOf
.map(schema => {
let name =
schema.typePrefix +
(schema.title ? `${schema.title} (${schema.displayType})` : schema.displayType);
if (name.indexOf(' or ') > -1) {
name = `(${name})`;
}
return name;
})
.join(' or ');
}
}
private initDiscriminator(

View File

@ -138,6 +138,7 @@ export interface OpenAPISchema {
maxProperties?: number;
minProperties?: number;
enum?: any[];
const?: any;
example?: any;
}