[asterisk-commits] dlee: branch dlee/ari-event-remodel2 r393218 - in /team/dlee/ari-event-remode...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Jun 28 15:37:07 CDT 2013


Author: dlee
Date: Fri Jun 28 15:37:05 2013
New Revision: 393218

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=393218
Log:
Adjusted for latest Swagger subtyping spec.

* Discriminator is now specified by a model field instead of a property type
* Actually, this is in swagger-1.3. Have no idea what swagger-1.2 was.

Modified:
    team/dlee/ari-event-remodel2/rest-api-templates/swagger_model.py
    team/dlee/ari-event-remodel2/rest-api/api-docs/events.json

Modified: team/dlee/ari-event-remodel2/rest-api-templates/swagger_model.py
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel2/rest-api-templates/swagger_model.py?view=diff&rev=393218&r1=393217&r2=393218
==============================================================================
--- team/dlee/ari-event-remodel2/rest-api-templates/swagger_model.py (original)
+++ team/dlee/ari-event-remodel2/rest-api-templates/swagger_model.py Fri Jun 28 15:37:05 2013
@@ -33,7 +33,8 @@
 import sys
 import traceback
 
-SWAGGER_VERSIONS = ["1.1", "1.2"]
+# I'm not quite sure what was in Swagger 1.2, but apparently I missed it
+SWAGGER_VERSIONS = ["1.1", "1.3"]
 
 SWAGGER_PRIMITIVES = [
     'void',
@@ -45,7 +46,6 @@
     'double',
     'float',
     'Date',
-    'DISCRIMINATOR',
 ]
 
 
@@ -54,6 +54,28 @@
     """
     def __repr__(self):
         return "%s(%s)" % (self.__class__, pprint.saferepr(self.__dict__))
+
+
+def compare_versions(lhs, rhs):
+    '''Performs a lexicographical comparison between two version numbers.
+
+    This properly handles simple major.minor.whatever.sure.why.not version
+    numbers, but fails miserably if there's any letters in there.
+
+    For reference:
+      1.0 == 1.0
+      1.0 < 1.0.1
+      1.2 < 1.10
+
+    @param lhs Left hand side of the comparison
+    @param rhs Right hand side of the comparison
+    @return  < 0 if lhs  < rhs
+    @return == 0 if lhs == rhs
+    @return  > 0 if lhs  > rhs
+    '''
+    lhs = [int(v) for v in lhs.split('.')]
+    rhs = [int(v) for v in rhs.split('.')]
+    return cmp(lhs, rhs)
 
 
 class ParsingContext(object):
@@ -81,8 +103,8 @@
 
     stack = property(get_stack)
 
-    def is_version(self, ver):
-        return self.swagger_version == ver
+    def version_less_than(self, ver):
+        return compare_versions(self.swagger_version, ver) < 0
 
     def next_stack(self, json, id_field):
         """Returns a new item pushed to the stack.
@@ -313,12 +335,6 @@
             raise SwaggerError("The type for integer should be 'int'", context)
 
         self.name = type_name
-        self.is_discriminator = self.name == 'DISCRIMINATOR'
-        if self.is_discriminator:
-            self.name = 'string'
-            if context.is_version("1.1"):
-                raise SwaggerError(
-                    "DISCRIMINATOR support added in Swagger 1.2", context)
         type_param = get_list_parameter_type(self.name)
         self.is_list = type_param is not None
         if self.is_list:
@@ -450,9 +466,6 @@
 
         type = property_json.get('type')
         self.type = type and SwaggerType().load(type, processor, context)
-        if self.type.is_discriminator:
-            # Discriminators are always required
-            self.required = True
 
         processor.process_property(self, context)
         return self
@@ -484,8 +497,8 @@
         if id != self.id:
             raise SwaggerError("Model id doesn't match name", context)
         self.extends = model_json.get('extends')
-        if self.extends and context.is_version("1.1"):
-            raise SwaggerError("Type extension support added in Swagger 1.2",
+        if self.extends and context.version_less_than("1.3"):
+            raise SwaggerError("Type extension support added in Swagger 1.3",
                                context)
         self.description = model_json.get('description')
         props = model_json.get('properties').items() or []
@@ -493,14 +506,21 @@
             Property(k).load(j, processor, context) for (k, j) in props]
         self.__properties = sorted(self.__properties, key=lambda p: p.name)
 
-        discriminators = [
-            p for p in self.__properties if p.type.is_discriminator]
-        if len(discriminators) > 1:
-            raise SwaggerError("Model may have at most one discriminator",
-                               context)
-
-        if discriminators:
-            self.__discriminator = discriminators[0]
+        discriminator = model_json.get('discriminator')
+
+        if discriminator:
+            if context.version_less_than("1.3"):
+                raise SwaggerError("Discriminator support added in Swagger 1.3",
+                                   context)
+
+            discr_props = [p for p in self.__properties if p.name == discriminator]
+            if not discr_props:
+                raise SwaggerError(
+                    "Discriminator '%s' does not name a property of '%s'" % (
+                        discriminator, self.id),
+                    context)
+
+            self.__discriminator = discr_props[0]
 
         self.model_json = json.dumps(model_json,
                                      indent=2, separators=(',', ': '))
@@ -602,7 +622,7 @@
         context = context.next(version=self.swagger_version)
         if not self.swagger_version in SWAGGER_VERSIONS:
             raise SwaggerError(
-                "Unsupported Swagger version %s" % swagger_version, context)
+                "Unsupported Swagger version %s" % self.swagger_version, context)
 
         validate_required_fields(api_decl_json, self.required_fields, context)
 

Modified: team/dlee/ari-event-remodel2/rest-api/api-docs/events.json
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ari-event-remodel2/rest-api/api-docs/events.json?view=diff&rev=393218&r1=393217&r2=393218
==============================================================================
--- team/dlee/ari-event-remodel2/rest-api/api-docs/events.json (original)
+++ team/dlee/ari-event-remodel2/rest-api/api-docs/events.json Fri Jun 28 15:37:05 2013
@@ -3,7 +3,7 @@
 	"_author": "David M. Lee, II <dlee at digium.com>",
 	"_svn_revision": "$Revision$",
 	"apiVersion": "0.0.1",
-	"swaggerVersion": "1.2",
+	"swaggerVersion": "1.3",
 	"basePath": "http://localhost:8088/stasis",
 	"resourcePath": "/api-docs/events.{format}",
 	"apis": [
@@ -36,9 +36,11 @@
 		"Event": {
 			"id": "Event",
 			"description": "Base type for asynchronous events from Asterisk.",
+			"discriminator": "type",
 			"properties": {
 				"type": {
-					"type": "DISCRIMINATOR",
+					"type": "string",
+					"required": true,
 					"description": "Indicates the type of this event."
 				},
 				"application": {




More information about the asterisk-commits mailing list