|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +import yaml |
| 4 | +import glob |
| 5 | +import sys |
| 6 | +import json |
| 7 | +import re |
| 8 | +import os |
| 9 | +from copy import deepcopy |
| 10 | +import dereferenceAll |
| 11 | + |
| 12 | +def buildJSONSchemas(parent, verbose): |
| 13 | + |
| 14 | + if 'schemas' in parent['components']: |
| 15 | + for schemaName in parent['components']['schemas']: |
| 16 | + if verbose: |
| 17 | + print('Data model: ' + schemaName) |
| 18 | + title = schemaName |
| 19 | + schema = parent['components']['schemas'][schemaName] |
| 20 | + module = '' |
| 21 | + if 'x-brapi-metadata' in schema: |
| 22 | + if 'primaryModel' in schema['x-brapi-metadata'] and schema['x-brapi-metadata']['primaryModel']: |
| 23 | + if verbose: |
| 24 | + print('-- Added') |
| 25 | + if 'title' in schema['x-brapi-metadata']: |
| 26 | + title = schema['x-brapi-metadata']['title'] |
| 27 | + if 'module' in schema['x-brapi-metadata']: |
| 28 | + module = schema['x-brapi-metadata']['module'] |
| 29 | + buildJSONSchema(schema, title, module, verbose) |
| 30 | + |
| 31 | + else: |
| 32 | + if verbose: |
| 33 | + print('-- Skipped') |
| 34 | + |
| 35 | + else: |
| 36 | + print("Error: no schemas found") |
| 37 | + |
| 38 | + |
| 39 | +def buildJSONSchema(schema, title, module, verbose): |
| 40 | + schema = fixSchema(schema) |
| 41 | + schemaObj = { |
| 42 | + "$schema": "http://json-schema.org/draft/2020-12/schema", |
| 43 | + "$id": "https://brapi.org/Specification/BrAPI-Schema/" + module + "/" + title +".json", |
| 44 | + "$defs": { |
| 45 | + title : { |
| 46 | + "title": title, |
| 47 | + "type": "object", |
| 48 | + "properties": schema["properties"] |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if 'required' in schema: |
| 54 | + schemaObj['$defs'][title]['required'] = schema['required'] |
| 55 | + |
| 56 | + filename = outPath + module + '/' + title + '.json' |
| 57 | + os.makedirs(os.path.dirname(filename), exist_ok=True) |
| 58 | + with open(filename, 'w') as outfile: |
| 59 | + json.dump(schemaObj, outfile, indent=4, sort_keys=True) |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +def fixSchema(schema): |
| 64 | + schema = addMinItems(schema) |
| 65 | + schema = removeDeprecated(schema) |
| 66 | + schema = removeSwaggerTerms(schema) |
| 67 | + schema = fixStringFormats(schema) |
| 68 | + schema = allowNullFields(schema) |
| 69 | + # schema = addRequired(schema) |
| 70 | + schema = addRefs(schema) |
| 71 | + return schema |
| 72 | + |
| 73 | +def addRefs(parent): |
| 74 | + newParent = deepcopy(parent) |
| 75 | + if type(parent) is dict: |
| 76 | + if 'properties' in newParent: |
| 77 | + className = '' |
| 78 | + if 'x-brapi-metadata' in newParent: |
| 79 | + className = newParent['x-brapi-metadata']['title'] |
| 80 | + for term in parent['properties']: |
| 81 | + if term.endswith('DbId') and (term.casefold() != (className + 'DbId').casefold()): |
| 82 | + oldTermDetails = newParent['properties'].pop(term) |
| 83 | + newTerm = term[:-4] |
| 84 | + newTermTitle = newTerm[0].upper() + newTerm[1:] |
| 85 | + if newTerm + 'Name' in newParent['properties']: |
| 86 | + newParent['properties'].pop(newTerm + 'Name') |
| 87 | + newParent['properties'][newTerm] = { '$ref': newTermTitle + '.json#/$defs/' + newTermTitle, 'description': oldTermDetails['description'], 'relationshipType': 'EDITME-to-one', 'referencedAttribute': className[0].lower() + className[1:] + 's' } |
| 88 | + elif term.endswith('DbIds') and (term.casefold() != (className + 'DbIds').casefold()): |
| 89 | + oldTermDetails = newParent['properties'].pop(term) |
| 90 | + newTerm = term[:-5] |
| 91 | + newTermTitle = newTerm[0].upper() + newTerm[1:] |
| 92 | + newParent['properties'][newTerm + 's'] = { 'items':{ '$ref': newTermTitle + '.json#/$defs/' + newTermTitle }, 'type': 'array', 'description': oldTermDetails['description'], 'relationshipType': 'EDITME-to-many', 'referencedAttribute': className[0].lower() + className[1:] + 's' } |
| 93 | + |
| 94 | + return newParent |
| 95 | + |
| 96 | +def addMinItems(parent): |
| 97 | + newParent = deepcopy(parent) |
| 98 | + if 'data' in newParent['properties']: |
| 99 | + newParent['properties']['data']['minItems'] = 1 |
| 100 | + return newParent |
| 101 | + |
| 102 | +def removeDeprecated(parent): |
| 103 | + newParent = deepcopy(parent) |
| 104 | + if type(parent) is dict: |
| 105 | + for childKey in parent: |
| 106 | + child = parent[childKey] |
| 107 | + if type(child) is dict: |
| 108 | + if 'deprecated' in child and child['deprecated']: |
| 109 | + newParent.pop(childKey) |
| 110 | + else: |
| 111 | + newParent[childKey] = removeDeprecated(child) |
| 112 | + return newParent |
| 113 | + |
| 114 | +def addRequired(parent): |
| 115 | + newParent = deepcopy(parent) |
| 116 | + if type(newParent) is dict: |
| 117 | + if 'properties' in newParent: |
| 118 | + if 'required' in parent: |
| 119 | + newParent['required'] = list(newParent['properties'].keys()); |
| 120 | + for childKey in newParent: |
| 121 | + newParent[childKey] = addRequired(newParent[childKey]) |
| 122 | + |
| 123 | + return newParent |
| 124 | + |
| 125 | +def removeSwaggerTerms(parent): |
| 126 | + newParent = deepcopy(parent) |
| 127 | + if type(newParent) is dict: |
| 128 | + if 'example' in newParent: |
| 129 | + newParent.pop('example') |
| 130 | + if 'discriminator' in newParent: |
| 131 | + newParent.pop('discriminator') |
| 132 | + for childKey in newParent: |
| 133 | + newParent[childKey] = removeSwaggerTerms(newParent[childKey]) |
| 134 | + elif type(newParent) is list: |
| 135 | + newList = [] |
| 136 | + for item in newParent: |
| 137 | + newList.append(removeSwaggerTerms(item)) |
| 138 | + newParent = newList |
| 139 | + return newParent |
| 140 | + |
| 141 | +def fixStringFormats(parent): |
| 142 | + newParent = deepcopy(parent) |
| 143 | + if type(parent) is dict: |
| 144 | + for childKey in parent: |
| 145 | + child = parent[childKey] |
| 146 | + if type(child) is dict: |
| 147 | + if 'format' in child and 'type' in child: |
| 148 | + if child['format'] != 'date-time' and child['format'] != 'uri': |
| 149 | + newParent[childKey].pop('format') |
| 150 | + else: |
| 151 | + newParent[childKey] = fixStringFormats(child) |
| 152 | + return newParent |
| 153 | + |
| 154 | +def allowNullFields(parent): |
| 155 | + newParent = deepcopy(parent) |
| 156 | + if type(parent) is dict: |
| 157 | + if 'properties' in parent: |
| 158 | + |
| 159 | + requiredFields = [] |
| 160 | + if 'required' in parent: |
| 161 | + requiredFields = parent['required'] |
| 162 | + |
| 163 | + for fieldName in parent['properties']: |
| 164 | + fieldObj = parent['properties'][fieldName] |
| 165 | + newParent['properties'][fieldName] = allowNullFields(fieldObj) |
| 166 | + |
| 167 | + if not fieldName in requiredFields: |
| 168 | + if 'enum' in fieldObj: |
| 169 | + newParent['properties'][fieldName]['enum'].append(None) |
| 170 | + types = ['null'] |
| 171 | + if 'type' in fieldObj: |
| 172 | + types.append(fieldObj['type']) |
| 173 | + newParent['properties'][fieldName]['type'] = types |
| 174 | + else: |
| 175 | + for fieldName in parent: |
| 176 | + if type(parent[fieldName]) is dict: |
| 177 | + newParent[fieldName] = allowNullFields(parent[fieldName]) |
| 178 | + |
| 179 | + return newParent |
| 180 | + |
| 181 | + |
| 182 | +outPath = './out/' |
| 183 | +yamlPath = './brapi_openapi.yaml' |
| 184 | +verbose = False |
| 185 | + |
| 186 | +verbose = '-v' in sys.argv |
| 187 | + |
| 188 | +if '-brapi' in sys.argv: |
| 189 | + bi = sys.argv.index('-brapi') |
| 190 | + yamlPath = sys.argv[bi + 1] |
| 191 | + |
| 192 | +if '-out' in sys.argv: |
| 193 | + ri = sys.argv.index('-out') |
| 194 | + outPath = sys.argv[ri + 1] |
| 195 | + |
| 196 | +if outPath[-1] != '/': |
| 197 | + outPath = outPath + '/' |
| 198 | + |
| 199 | +if verbose: |
| 200 | + print('input YAML file: ' + yamlPath) |
| 201 | + print('output directory: ' + outPath) |
| 202 | + |
| 203 | +parentFile = dereferenceAll.dereferenceBrAPI(yamlPath, verbose) |
| 204 | + |
| 205 | +buildJSONSchemas(parentFile, verbose) |
0 commit comments