From 2fbcbf9d88c8efe6c94c940623ac3a17d578e49e Mon Sep 17 00:00:00 2001 From: Pavel Bodiachevskii Date: Sat, 20 Apr 2024 14:20:30 +0400 Subject: [PATCH] refactor(avro schema): check schemas before merge https://github.com/asyncapi/jasyncapi/issues/185 --- .../v3/schema/avro/v1/_9_0/AvroSchema.java | 33 ++++++++++++++++++- .../schema/avro/v1/_9_0/AvroSchemaArray.java | 3 ++ .../schema/avro/v1/_9_0/AvroSchemaEnum.java | 2 ++ .../schema/avro/v1/_9_0/AvroSchemaFixed.java | 2 ++ .../schema/avro/v1/_9_0/AvroSchemaRecord.java | 15 ++++----- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchema.java b/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchema.java index 9f11ed4a..df2d7734 100644 --- a/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchema.java +++ b/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchema.java @@ -1,5 +1,6 @@ package com.asyncapi.v3.schema.avro.v1._9_0; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonSubTypes; import com.fasterxml.jackson.annotation.JsonTypeInfo; import lombok.Data; @@ -58,19 +59,49 @@ public AvroSchema(@NotNull Builder builder) { * Avro Schema type. */ @NotNull + @JsonProperty("type") private String type; + /** + * A JSON integer representing the scale (optional). + *

+ * If not specified the scale is 0. + */ @Nullable + @JsonProperty("scale") private Integer scale; /* - Type: bytes, fixed + Type: bytes, fixed, decimal */ + /** + * A JSON integer representing the (maximum) precision of decimals stored in this type (required). + */ @Nullable + @JsonProperty("precision") private Integer precision; + /** + * A logical type is an Avro primitive or complex type with extra attributes to represent a derived type. + *

+ * The attribute logicalType must always be present for a logical type, and is a string with the name of one + * of the logical types listed later in this section. Other attributes may be defined for particular logical types. + *

+ *

+ * A logical type is always serialized using its underlying Avro type so that values are encoded in exactly the same + * way as the equivalent Avro type that does not have a logicalType attribute. Language implementations may choose to + * represent logical types with an appropriate native type, although this is not required. + *

+ * Language implementations must ignore unknown logical types when reading, and should use the underlying Avro type. + *

+ * If a logical type is invalid, for example a decimal with scale greater than its precision, then implementations + * should ignore the logical type and use the underlying Avro type. + * + * @see Logical Types + */ @Nullable + @JsonProperty("logicalType") private String logicalType; public static Builder builder() { diff --git a/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaArray.java b/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaArray.java index 9b2959a9..a51a1c4e 100644 --- a/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaArray.java +++ b/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaArray.java @@ -47,6 +47,9 @@ public AvroSchemaArray(@NotNull Builder builder) { this.metadata = builder.metadata; } + /** + * The schema of the array's items. + */ @NotNull @JsonProperty("items") @JsonDeserialize(using = AvroTypeDeserializer.class) diff --git a/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaEnum.java b/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaEnum.java index 2db885ac..965239d8 100644 --- a/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaEnum.java +++ b/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaEnum.java @@ -69,6 +69,7 @@ public AvroSchemaEnum(@NotNull Builder builder) { * A JSON string providing documentation to the user of this schema (optional). */ @Nullable + @JsonProperty("doc") private String doc; /** @@ -79,6 +80,7 @@ public AvroSchemaEnum(@NotNull Builder builder) { * Every symbol must match the regular expression [A-Za-z_][A-Za-z0-9_]* (the same requirement as for names). */ @NotNull + @JsonProperty("symbols") private List<@NotNull String> symbols = Collections.emptyList(); /** diff --git a/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaFixed.java b/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaFixed.java index f4090fb7..18e8abc9 100644 --- a/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaFixed.java +++ b/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaFixed.java @@ -61,12 +61,14 @@ public AvroSchemaFixed(@NotNull Builder builder) { * A JSON array of strings, providing alternate names for this record (optional). */ @Nullable + @JsonProperty("aliases") private List<@NotNull String> aliases; /** * An integer, specifying the number of bytes per value (required). */ @NotNull + @JsonProperty("size") private Integer size; @NotNull diff --git a/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaRecord.java b/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaRecord.java index 1dd0066a..684742e6 100644 --- a/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaRecord.java +++ b/asyncapi-core/src/main/java/com/asyncapi/v3/schema/avro/v1/_9_0/AvroSchemaRecord.java @@ -1,5 +1,6 @@ package com.asyncapi.v3.schema.avro.v1._9_0; +import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; import lombok.EqualsAndHashCode; import org.jetbrains.annotations.NotNull; @@ -62,30 +63,35 @@ public AvroSchemaRecord(@NotNull Builder builder) { * A JSON string providing the name of the record (required). */ @NotNull + @JsonProperty("name") private String name = ""; /** * A JSON string that qualifies the name. */ @Nullable + @JsonProperty("namespace") private String namespace; /** * A JSON string providing documentation to the user of this schema (optional). */ @Nullable + @JsonProperty("doc") private String doc; /** * A JSON array of strings, providing alternate names for this record (optional). */ @Nullable + @JsonProperty("aliases") private List<@NotNull String> aliases; /** * A JSON array, listing fields (required). */ @NotNull + @JsonProperty("fields") private List<@NotNull AvroSchemaRecordField> fields = Collections.emptyList(); @NotNull @@ -114,9 +120,6 @@ public static class Builder extends AvroSchema.Builder { @Nullable private String doc; - @NotNull - private List<@NotNull String> symbols = Collections.emptyList(); - @Nullable private List<@NotNull String> aliases; @@ -141,12 +144,6 @@ public Builder doc(@Nullable String doc) { return this; } - @NotNull - public Builder symbols(@NotNull List<@NotNull String> symbols) { - this.symbols = symbols; - return this; - } - @NotNull public Builder aliases(@NotNull List<@NotNull String> aliases) { this.aliases = aliases;