Filter readOnly and writeOnly fields from request/response

This commit is contained in:
Roman Hotsiy 2017-11-20 12:44:55 +02:00
parent f65dd955fa
commit 5438770267
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
8 changed files with 46 additions and 4 deletions

View File

@ -748,6 +748,7 @@ components:
Id:
type: integer
format: int64
readOnly: true
Order:
type: object
properties:

View File

@ -20,7 +20,7 @@ const renderRoot = (Component: typeof Redoc, props: { store: AppStore }) =>
const big = window.location.search.indexOf('big') > -1;
const swagger = window.location.search.indexOf('swagger') > -1; //compatibility mode ?
const specUrl = swagger ? 'swagger.yaml' : big ? 'big-openapi.json' : 'swagger.yaml';
const specUrl = swagger ? 'swagger.yaml' : big ? 'big-openapi.json' : 'openapi.yaml';
let store;

View File

@ -66,7 +66,7 @@ function BodyContent(props: { content: MediaContentModel }): JSX.Element {
)}
>
{({ schema }) => {
return <Schema key="schema" schema={schema} />;
return <Schema skipReadOnly={true} key="schema" schema={schema} />;
}}
</MediaTypesSwitch>
);

View File

@ -46,7 +46,7 @@ export class ResponseView extends React.Component<{ response: ResponseModel }> {
)}
>
{({ schema }) => {
return <Schema key="schema" schema={schema} />;
return <Schema skipWriteOnly={true} key="schema" schema={schema} />;
}}
</MediaTypesSwitch>
</ResponseDetailsWrap>

View File

@ -62,11 +62,22 @@ export class ObjectSchema extends React.Component<ObjectSchemaProps> {
render() {
const { schema: { fields = [] }, showTitle, discriminator } = this.props;
const needFilter = this.props.skipReadOnly || this.props.skipWriteOnly;
const filteredFields = needFilter
? fields.filter(item => {
return (
(this.props.skipReadOnly && !item.schema.readOnly) ||
(this.props.skipWriteOnly && !item.schema.writeOnly)
);
})
: fields;
return (
<PropertiesTable>
{showTitle && <PropertiesTableCaption>{this.props.schema.title}</PropertiesTableCaption>}
<tbody>
{mapWithLast(fields, (field, isLast) =>
{mapWithLast(filteredFields, (field, isLast) =>
this.renderField(
field,
isLast,

View File

@ -13,6 +13,8 @@ import { ArraySchema } from './ArraySchema';
export interface SchemaProps {
schema: SchemaModel;
showTitle?: boolean;
skipReadOnly?: boolean;
skipWriteOnly?: boolean;
}
@observer

View File

@ -59,6 +59,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"default": undefined,
"type": "number",
},
"readOnly": false,
"schema": Object {
"default": undefined,
"type": "number",
@ -66,6 +67,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"title": "",
"type": "number",
"typePrefix": "",
"writeOnly": false,
},
},
FieldModel {
@ -95,6 +97,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"default": undefined,
"type": "string",
},
"readOnly": false,
"schema": Object {
"default": undefined,
"type": "string",
@ -102,6 +105,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"title": "",
"type": "string",
"typePrefix": "",
"writeOnly": false,
},
},
],
@ -123,6 +127,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
},
"type": "object",
},
"readOnly": false,
"schema": Object {
"allOf": undefined,
"discriminator": Object {
@ -148,6 +153,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"title": "Dog",
"type": "object",
"typePrefix": "",
"writeOnly": false,
},
SchemaModel {
"_$ref": "#/components/schemas/Cat",
@ -187,6 +193,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"default": undefined,
"type": "string",
},
"readOnly": false,
"schema": Object {
"default": undefined,
"type": "string",
@ -194,6 +201,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"title": "",
"type": "string",
"typePrefix": "",
"writeOnly": false,
},
},
FieldModel {
@ -223,6 +231,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"default": undefined,
"type": "number",
},
"readOnly": false,
"schema": Object {
"default": undefined,
"type": "number",
@ -230,6 +239,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"title": "",
"type": "number",
"typePrefix": "",
"writeOnly": false,
},
},
],
@ -253,6 +263,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
],
"type": "object",
},
"readOnly": false,
"schema": Object {
"allOf": undefined,
"discriminator": Object {
@ -278,6 +289,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"title": "Cat",
"type": "object",
"typePrefix": "",
"writeOnly": false,
},
],
"pattern": undefined,
@ -295,6 +307,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
],
"type": "object",
},
"readOnly": false,
"schema": Object {
"discriminator": Object {
"propertyName": "type",
@ -312,6 +325,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"title": "Pet",
"type": "object",
"typePrefix": "",
"writeOnly": false,
},
}
}
@ -354,6 +368,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"default": undefined,
"type": "number",
},
"readOnly": false,
"schema": Object {
"default": undefined,
"type": "number",
@ -361,6 +376,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"title": "",
"type": "number",
"typePrefix": "",
"writeOnly": false,
},
},
FieldModel {
@ -390,6 +406,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"default": undefined,
"type": "string",
},
"readOnly": false,
"schema": Object {
"default": undefined,
"type": "string",
@ -397,6 +414,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"title": "",
"type": "string",
"typePrefix": "",
"writeOnly": false,
},
},
],
@ -418,6 +436,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
},
"type": "object",
},
"readOnly": false,
"schema": Object {
"allOf": undefined,
"discriminator": Object {
@ -443,6 +462,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"title": "Dog",
"type": "object",
"typePrefix": "",
"writeOnly": false,
}
}
/>
@ -480,6 +500,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"default": undefined,
"type": "number",
},
"readOnly": false,
"schema": Object {
"default": undefined,
"type": "number",
@ -487,6 +508,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"title": "",
"type": "number",
"typePrefix": "",
"writeOnly": false,
},
}
}
@ -523,6 +545,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"default": undefined,
"type": "string",
},
"readOnly": false,
"schema": Object {
"default": undefined,
"type": "string",
@ -530,6 +553,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"title": "",
"type": "string",
"typePrefix": "",
"writeOnly": false,
},
}
}

View File

@ -33,6 +33,8 @@ export class SchemaModel {
example?: any;
enum: any[];
default?: any;
readOnly: boolean;
writeOnly: boolean;
constraints: string[];
@ -101,6 +103,8 @@ export class SchemaModel {
this.displayType = this.title === '' ? this.type : `${this.title} (${this.type})`;
this.isPrimitive = isPrimitiveType(schema);
this.default = schema.default;
this.readOnly = !!schema.readOnly;
this.writeOnly = !!schema.writeOnly;
if (this.isCircular) {
return;