Generate a OpenAPI schema.
(self, request=None, public=False)
| 62 | } |
| 63 | |
| 64 | def get_schema(self, request=None, public=False): |
| 65 | """ |
| 66 | Generate a OpenAPI schema. |
| 67 | """ |
| 68 | self._initialise_endpoints() |
| 69 | components_schemas = {} |
| 70 | |
| 71 | # Iterate endpoints generating per method path operations. |
| 72 | paths = {} |
| 73 | _, view_endpoints = self._get_paths_and_endpoints(None if public else request) |
| 74 | for path, method, view in view_endpoints: |
| 75 | if not self.has_view_permissions(path, method, view): |
| 76 | continue |
| 77 | |
| 78 | operation = view.schema.get_operation(path, method) |
| 79 | components = view.schema.get_components(path, method) |
| 80 | for k in components.keys(): |
| 81 | if k not in components_schemas: |
| 82 | continue |
| 83 | if components_schemas[k] == components[k]: |
| 84 | continue |
| 85 | warnings.warn(f'Schema component "{k}" has been overridden with a different value.') |
| 86 | |
| 87 | components_schemas.update(components) |
| 88 | |
| 89 | # Normalize path for any provided mount url. |
| 90 | if path.startswith('/'): |
| 91 | path = path[1:] |
| 92 | path = urljoin(self.url or '/', path) |
| 93 | |
| 94 | paths.setdefault(path, {}) |
| 95 | paths[path][method.lower()] = operation |
| 96 | |
| 97 | self.check_duplicate_operation_id(paths) |
| 98 | |
| 99 | # Compile final schema. |
| 100 | schema = { |
| 101 | 'openapi': '3.0.2', |
| 102 | 'info': self.get_info(), |
| 103 | 'paths': paths, |
| 104 | } |
| 105 | |
| 106 | if len(components_schemas) > 0: |
| 107 | schema['components'] = { |
| 108 | 'schemas': components_schemas |
| 109 | } |
| 110 | |
| 111 | return schema |
| 112 | |
| 113 | # View Inspectors |
| 114 |