def get_additional_properties(self, _type, *args, **kwargs): """Make head and table with additional properties by schema_id :param str _type: :rtype: str """ if not SchemaObjects.contains(_type): return _type schema = SchemaObjects.get(_type) body = [] for sch in schema.nested_schemas: # complex types nested_schema = SchemaObjects.get(sch) if not (nested_schema or isinstance(nested_schema, SchemaMapWrapper)): continue body.append('Map of {{"key":"{}"}}\n\n'.format(self.get_type_description( nested_schema.schema_id, *args, **kwargs)) # head ) if nested_schema.is_array: # table _schema = SchemaObjects.get(nested_schema.item.get('type')) if _schema and _schema.schema_type == SchemaTypes.INLINE: body.append(self.get_regular_properties(_schema.schema_id, *args, **kwargs)) else: body.append(self.get_regular_properties(nested_schema.schema_id, *args, **kwargs)) if schema.type_format: # basic types, only head body.append( 'Map of {{"key":"{}"}}'.format(self.get_type_description(schema.type_format, *args, **kwargs))) return ''.join(body)
def get_regular_properties(self, _type, *args, **kwargs): """Make table with properties by schema_id :param str _type: :rtype: str """ if not SchemaObjects.contains(_type): return _type schema = SchemaObjects.get(_type) if schema.schema_type == SchemaTypes.DEFINITION and not kwargs.get('definition'): return '' head = """.. csv-table:: :delim: | :header: "Name", "Required", "Type", "Format", "Properties", "Description" :widths: 20, 10, 15, 15, 30, 25 """ body = [] if schema.properties: for p in schema.properties: body.append(' {} | {} | {} | {} | {} | {}'.format( p.get('name') or '', 'Yes' if p.get('required') else 'No', self.get_type_description(p['type'], *args, **kwargs), p.get('type_format') or '', '{}'.format(p.get('type_properties') or ''), p.get('description') or '') ) body.sort() return (head + '\n'.join(body))
def get_property_example(cls, property_, nested=None, **kw): """ Get example for property :param dict property_: :param set nested: :return: example value """ paths = kw.get('paths', []) name = kw.get('name', '') result = None if name and paths: paths = list(map(lambda path: '.'.join((path, name)), paths)) result, path = cls._get_custom_example(paths) if result is not None and property_['type'] in PRIMITIVE_TYPES: cls._example_validate( path, result, property_['type'], property_['type_format']) return result if SchemaObjects.contains(property_['type']): schema = SchemaObjects.get(property_['type']) if result is not None: if schema.is_array: if not isinstance(result, list): result = [result] * cls.EXAMPLE_ARRAY_ITEMS_COUNT else: if isinstance(result, list): cls.logger.warning( 'Example type mismatch in path {}'.format(schema.ref_path)) else: result = cls.get_example_by_schema(schema, **kw) if (not result) and schema.nested_schemas: for _schema_id in schema.nested_schemas: _schema = SchemaObjects.get(_schema_id) if _schema: if isinstance(_schema, SchemaMapWrapper): result[_schema.name] = cls.get_example_by_schema(_schema, **kw) elif _schema.nested_schemas: for _schema__id in _schema.nested_schemas: _schema_ = SchemaObjects.get(_schema__id) if isinstance(_schema_, SchemaMapWrapper): result[_schema.name] = cls.get_example_by_schema(_schema_, **kw) else: result = cls.get_example_value_for_primitive_type( property_['type'], property_['type_properties'], property_['type_format'], **kw ) return result
def get_type_description(self, _type, suffix='', *args, **kwargs): """ Get description of type :param suffix: :param str _type: :rtype: str """ if not SchemaObjects.contains(_type): return _type schema = SchemaObjects.get(_type) if schema.all_of: models = ','.join( (self.get_type_description(_type, *args, **kwargs) for _type in schema.all_of) ) result = '{}'.format(models.split(',')[0]) for r in models.split(',')[1:]: result += ' extended {}'.format(r) elif schema.is_array: result = 'array of {}'.format( self.get_type_description(schema.item['type'], *args, **kwargs)) else: result = ':ref:`{} <{}{}>`'.format(schema.name, schema.schema_id, suffix) return result