def _extract_schemas(self, obj):
     """Converts all schemes in a given object to its proper swagger representation."""
     definitions = {}
     if isinstance(obj, list):
         for i, o in enumerate(obj):
             obj[i], definitions_ = self._extract_schemas(o)
             definitions.update(definitions_)
     if isinstance(obj, dict):
         for k, v in obj.items():
             obj[k], definitions_ = self._extract_schemas(v)
             definitions.update(definitions_)
     if inspect.isclass(obj):
         # Object is a model. Convert it to valid json and get a definition object
         if not issubclass(obj, Schema):
             raise ValueError('"{0}" is not a subclass of the schema model'.format(obj))
         definition = obj.definitions()
         description = parse_schema_doc(obj, definition)
         if description:
             definition['description'] = description
         # The definition itself might contain models, so extract them again
         definition, additional_definitions = self._extract_schemas(definition)
         definitions[obj.__name__] = definition
         definitions.update(additional_definitions)
         obj = obj.reference()
     return obj, definitions
    def _extract_schemas(self, obj):
        """Converts all schemes in a given object to its proper swagger representation."""
        definitions = {}
        if isinstance(obj, list):
            for i, o in enumerate(obj):
                obj[i], definitions_ = self._extract_schemas(o)
                definitions.update(definitions_)

        if isinstance(obj, dict):
            for k, v in obj.items():
                obj[k], definitions_ = self._extract_schemas(v)
                definitions.update(definitions_)

        if inspect.isclass(obj):
            # Object is a model. Convert it to valid json and get a definition object
            if not issubclass(obj, Schema):
                raise ValueError(
                    '"{0}" is not a subclass of the schema model'.format(obj))
            definition = obj.definitions()
            description = parse_schema_doc(obj, definition)
            if description:
                definition['description'] = description
            # The definition itself might contain models, so extract them again
            definition, additional_definitions = self._extract_schemas(
                definition)
            definitions[obj.__name__] = definition
            definitions.update(additional_definitions)
            obj = obj.reference()

        return obj, definitions
 def _extract_model(self, obj, definitions):
     # Object is a model. Convert it to valid json and get a definition object
     if not issubclass(obj, Schema):
         raise ValueError(
             '"{0}" is not a subclass of the schema model'.format(obj))
     definition = obj.definitions()
     description = parse_schema_doc(obj, definition)
     if description:
         definition['description'] = description
     # The definition itself might contain models, so extract them again
     definition, additional_definitions = self._extract_schemas(definition)
     definitions[obj.__name__] = definition
     definitions.update(additional_definitions)
     obj = obj.reference()
     return obj, definitions
Example #4
0
 def test_should_parse_schema_doc_existing_description(self):
     test_model = TestModel()
     self.assertIsNone(
         swagger.parse_schema_doc(test_model,
                                  {'description': 'Test description'}))
Example #5
0
 def test_should_parse_schema_doc(self):
     test_model = TestModel()
     self.assertEqual(swagger.parse_schema_doc(test_model, {}),
                      'Test schema model.')
 def test_should_parse_schema_doc_existing_description(self):
     test_model = TestModel()
     self.assertIsNone(swagger.parse_schema_doc(test_model,
                                                {'description': 'Test description'}))
 def test_should_parse_schema_doc(self):
     test_model = TestModel()
     self.assertEqual(swagger.parse_schema_doc(test_model, {}), 'Test schema model.')