|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.cdk.data |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.JsonNode |
| 8 | +import com.fasterxml.jackson.databind.node.JsonNodeFactory |
| 9 | +import com.fasterxml.jackson.databind.node.ObjectNode |
| 10 | + |
| 11 | +class JsonSchemaToAirbyteType { |
| 12 | + fun convert(schema: JsonNode): AirbyteType { |
| 13 | + // try { |
| 14 | + if (schema.isObject && schema.has("type")) { |
| 15 | + // Normal json object with {"type": ..., ...} |
| 16 | + val schemaType = (schema as ObjectNode).get("type") |
| 17 | + return if (schemaType.isTextual) { |
| 18 | + // {"type": <string>, ...} |
| 19 | + when (schema.get("type").asText()) { |
| 20 | + "string" -> fromString(schema) |
| 21 | + "boolean" -> BooleanType |
| 22 | + "integer" -> IntegerType |
| 23 | + "number" -> fromNumber(schema) |
| 24 | + "array" -> fromArray(schema) |
| 25 | + "object" -> fromObject(schema) |
| 26 | + "null" -> NullType |
| 27 | + else -> |
| 28 | + throw IllegalArgumentException( |
| 29 | + "Unknown type: ${ |
| 30 | + schema.get("type").asText() |
| 31 | + }" |
| 32 | + ) |
| 33 | + } |
| 34 | + } else if (schemaType.isArray) { |
| 35 | + // {"type": [...], ...} |
| 36 | + unionFromCombinedTypes(schemaType.toList(), schema) |
| 37 | + } else { |
| 38 | + UnknownType("unspported type for 'type' field: $schemaType") |
| 39 | + } |
| 40 | + } else if (schema.isObject) { |
| 41 | + // {"oneOf": [...], ...} or {"anyOf": [...], ...} or {"allOf": [...], ...} |
| 42 | + val options = schema.get("oneOf") ?: schema.get("anyOf") ?: schema.get("allOf") |
| 43 | + return if (options != null) { |
| 44 | + UnionType(options.map { convert(it as ObjectNode) }) |
| 45 | + } else { |
| 46 | + // Default to object if no type and not a union type |
| 47 | + convert((schema as ObjectNode).put("type", "object")) |
| 48 | + } |
| 49 | + } else if (schema.isTextual) { |
| 50 | + // "<typename>" |
| 51 | + val typeSchema = JsonNodeFactory.instance.objectNode().put("type", schema.asText()) |
| 52 | + return convert(typeSchema) |
| 53 | + } else { |
| 54 | + return UnknownType("Unknown schema type: $schema") |
| 55 | + } |
| 56 | + } // catch (t: Throwable) { |
| 57 | + // return UnknownType(t.message ?: "Unknown error") |
| 58 | + // } |
| 59 | + // } |
| 60 | + |
| 61 | + private fun fromString(schema: ObjectNode): AirbyteType = |
| 62 | + when (schema.get("format")?.asText()) { |
| 63 | + "date" -> DateType |
| 64 | + "time" -> |
| 65 | + TimeType( |
| 66 | + hasTimezone = schema.get("airbyte_type")?.asText() != "time_without_timezone" |
| 67 | + ) |
| 68 | + "date-time" -> |
| 69 | + TimestampType( |
| 70 | + hasTimezone = |
| 71 | + schema.get("airbyte_type")?.asText() != "timestamp_without_timezone" |
| 72 | + ) |
| 73 | + null -> StringType |
| 74 | + else -> |
| 75 | + throw IllegalArgumentException( |
| 76 | + "Unknown string format: ${ |
| 77 | + schema.get("format").asText() |
| 78 | + }" |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + private fun fromNumber(schema: ObjectNode): AirbyteType = |
| 83 | + if (schema.get("airbyte_type")?.asText() == "integer") { |
| 84 | + IntegerType |
| 85 | + } else { |
| 86 | + NumberType |
| 87 | + } |
| 88 | + |
| 89 | + private fun fromArray(schema: ObjectNode): AirbyteType { |
| 90 | + val items = schema.get("items") ?: return ArrayTypeWithoutSchema |
| 91 | + if (items.isArray) { |
| 92 | + if (items.isEmpty) { |
| 93 | + return ArrayTypeWithoutSchema |
| 94 | + } |
| 95 | + val itemOptions = UnionType(items.map { convert(it) }) |
| 96 | + return ArrayType(fieldFromUnion(itemOptions)) |
| 97 | + } |
| 98 | + return ArrayType(fieldFromSchema(items as ObjectNode)) |
| 99 | + } |
| 100 | + |
| 101 | + private fun fromObject(schema: ObjectNode): AirbyteType { |
| 102 | + val properties = schema.get("properties") ?: return ObjectTypeWithoutSchema |
| 103 | + if (properties.isEmpty) { |
| 104 | + return ObjectTypeWithEmptySchema |
| 105 | + } |
| 106 | + val requiredFields = schema.get("required")?.map { it.asText() } ?: emptyList() |
| 107 | + return objectFromProperties(properties as ObjectNode, requiredFields) |
| 108 | + } |
| 109 | + |
| 110 | + private fun fieldFromSchema( |
| 111 | + fieldSchema: ObjectNode, |
| 112 | + onRequiredList: Boolean = false |
| 113 | + ): FieldType { |
| 114 | + val markedRequired = fieldSchema.get("required")?.asBoolean() ?: false |
| 115 | + val nullable = !(onRequiredList || markedRequired) |
| 116 | + val airbyteType = convert(fieldSchema) |
| 117 | + if (airbyteType is UnionType) { |
| 118 | + return fieldFromUnion(airbyteType, nullable) |
| 119 | + } else { |
| 120 | + return FieldType(airbyteType, nullable) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private fun fieldFromUnion(unionType: UnionType, nullable: Boolean = false): FieldType { |
| 125 | + if (unionType.options.contains(NullType)) { |
| 126 | + val filtered = unionType.options.filter { it != NullType } |
| 127 | + return FieldType(UnionType(filtered), nullable = true) |
| 128 | + } |
| 129 | + return FieldType(unionType, nullable = nullable) |
| 130 | + } |
| 131 | + |
| 132 | + private fun objectFromProperties(schema: ObjectNode, requiredFields: List<String>): ObjectType { |
| 133 | + val properties = |
| 134 | + schema |
| 135 | + .fields() |
| 136 | + .asSequence() |
| 137 | + .map { (name, node) -> |
| 138 | + name to fieldFromSchema(node as ObjectNode, requiredFields.contains(name)) |
| 139 | + } |
| 140 | + .toMap(LinkedHashMap()) |
| 141 | + return ObjectType(properties) |
| 142 | + } |
| 143 | + |
| 144 | + private fun unionFromCombinedTypes( |
| 145 | + options: List<JsonNode>, |
| 146 | + parentSchema: ObjectNode |
| 147 | + ): UnionType { |
| 148 | + // Denormalize the properties across each type (the converter only checks what matters |
| 149 | + // per type). |
| 150 | + val unionOptions = |
| 151 | + options.map { |
| 152 | + if (it.isTextual) { |
| 153 | + val schema = parentSchema.deepCopy() |
| 154 | + schema.put("type", it.textValue()) |
| 155 | + convert(schema) |
| 156 | + } else { |
| 157 | + convert(it) |
| 158 | + } |
| 159 | + } |
| 160 | + return UnionType(unionOptions) |
| 161 | + } |
| 162 | +} |
0 commit comments