def _resolve_x_pattern(self, pattern_extension): """Find all instances of pattern_extension in the openapi content and generate a #/components/schemas/... pattern schema object that is specific to the property hosting the pattern extension content. Replace the x-field-pattern schema with a $ref to the generated schema. """ import jsonpath_ng for xpattern_path in self._get_parser( "$..{}".format(pattern_extension) ).find(self._content): print("generating %s..." % (str(xpattern_path.full_path))) object_name = xpattern_path.full_path.left.left.left.right.fields[ 0 ] property_name = xpattern_path.full_path.left.right.fields[0] property_schema = jsonpath_ng.Parent().find(xpattern_path)[0].value xpattern = xpattern_path.value schema_name = "Pattern.{}.{}".format( "".join( [ piece[0].upper() + piece[1:] for piece in object_name.split("_") ] ), "".join( [ piece[0].upper() + piece[1:] for piece in property_name.split("_") ] ), ) format = None type_name = xpattern["format"] if type_name in ["ipv4", "ipv6", "mac", "enum"]: format = type_name type_name = "string" description = "TBD" if "description" in xpattern: description = xpattern["description"] elif "description" in property_schema: description = property_schema["description"] if xpattern["format"] == "checksum": self._generate_checksum_schema( xpattern, schema_name, description ) else: self._generate_value_schema( xpattern, schema_name, description, type_name, format ) property_schema["$ref"] = "#/components/schemas/{}".format( schema_name ) del property_schema[pattern_extension]
def _resolve_x_include(self): """Find all instances of x-include in the openapi content and merge the x-include content into the parent object """ import jsonpath_ng for xincludes in jsonpath_ng.parse('$..x-include').find(self._content): print('resolving %s...' % (str(xincludes.full_path))) parent_schema_object = jsonpath_ng.Parent().find(xincludes)[0].value for xinclude in xincludes.value: include_schema_object = self._includes[xinclude] self._merge(copy.deepcopy(include_schema_object), parent_schema_object) del parent_schema_object['x-include']
def _resolve_x_constraint(self): """Find all instances of x-constraint in the openapi content and merge the x-constraint content into the parent object description """ import jsonpath_ng for xconstraint in jsonpath_ng.parse('$..x-constraint').find(self._content): print('resolving %s...' % (str(xconstraint.full_path))) parent_schema_object = jsonpath_ng.Parent().find(xconstraint)[0].value if 'description' not in parent_schema_object: parent_schema_object['description'] = 'TBD' parent_schema_object['description'] += '\n\nx-constraint:\n' for constraint in xconstraint.value: parent_schema_object['description'] += '- {}\n'.format(constraint)
def _resolve_x_include(self): """Find all instances of x-include in the openapi content and merge the x-include content into the parent object Remove the x-include and the included content """ include_schemas = [] for xincludes in self._get_parser("$..x-include").find(self._content): parent_schema_object = ( jsonpath_ng.Parent().find(xincludes)[0].value ) for xinclude in xincludes.value: print("resolving %s..." % (str(xinclude))) include_schemas.append(xinclude) include_schema_object = self._includes[xinclude] self._merge( copy.deepcopy(include_schema_object), parent_schema_object ) del parent_schema_object["x-include"]
def _resolve_x_status(self): """Find all instances of x-constraint in the openapi content and merge the x-constraint content into the parent object description """ import jsonpath_ng for xstatus in self._get_parser("$..x-status").find(self._content): if xstatus.value == "current": continue print("resolving %s..." % (str(xstatus.full_path))) parent_schema_object = jsonpath_ng.Parent().find(xstatus)[0].value if "description" not in parent_schema_object: parent_schema_object["description"] = "TBD" parent_schema_object[ "description" ] = "Status: {status}\n{description}".format( status=xstatus.value, description=parent_schema_object["description"], )
def _resolve_x_constraint(self): """Find all instances of x-constraint in the openapi content and merge the x-constraint content into the parent object description """ import jsonpath_ng for xconstraint in self._get_parser("$..x-constraint").find( self._content ): print("resolving %s..." % (str(xconstraint.full_path))) parent_schema_object = ( jsonpath_ng.Parent().find(xconstraint)[0].value ) if "description" not in parent_schema_object: parent_schema_object["description"] = "TBD" parent_schema_object["description"] += "\n\nx-constraint:\n" for constraint in xconstraint.value: parent_schema_object["description"] += "- {}\n".format( constraint )
def _resolve_x_pattern(self, pattern_extension): """Find all instances of pattern_extension in the openapi content and generate a #/components/schemas/... pattern schema object that is specific to the property hosting the pattern extension content. Replace the x-field-pattern schema with a $ref to the generated schema. """ import jsonpath_ng for xpattern_path in jsonpath_ng.parse('$..{}'.format(pattern_extension)).find(self._content): print('generating %s...' % (str(xpattern_path.full_path))) object_name = xpattern_path.full_path.left.left.left.right.fields[0] property_name = xpattern_path.full_path.left.right.fields[0] property_schema = jsonpath_ng.Parent().find(xpattern_path)[0].value xpattern = xpattern_path.value schema_name = 'Pattern.{}.{}'.format( ''.join([piece[0].upper() + piece[1:] for piece in object_name.split('_')]), ''.join([piece[0].upper() + piece[1:] for piece in property_name.split('_')]) ) format = None type_name = xpattern['format'] if type_name in ['ipv4', 'ipv6', 'mac', 'enum']: format = type_name type_name = 'string' description = 'TBD' if 'description' in xpattern: description = xpattern['description'] elif 'description' in property_schema: description = property_schema['description'] if xpattern['format'] == 'checksum': self._generate_checksum_schema(xpattern, schema_name, description) else: self._generate_value_schema(xpattern, schema_name, description, type_name, format) property_schema['$ref'] = '#/components/schemas/{}'.format( schema_name ) del property_schema[pattern_extension]