Skip to content

Commit

Permalink
chore: Improved AsyncAPI interface
Browse files Browse the repository at this point in the history
Improved the creation of Schemas.

A 'Schemas' is now an interface that's implemented by
'SchemasObject' and 'SchemasReference'. This change
allows to properly define a 'Schemas' and use it in a more
transparent way.

Added also different builders
  • Loading branch information
ctasada committed Dec 28, 2023
1 parent ca8dc52 commit 2a75dc4
Show file tree
Hide file tree
Showing 15 changed files with 227 additions and 252 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import io.github.stavshamir.springwolf.asyncapi.v3.bindings.ChannelBinding;
import io.github.stavshamir.springwolf.asyncapi.v3.model.Reference;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.Schema;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
Expand Down Expand Up @@ -38,7 +37,7 @@ public class MQTTMessageBinding extends ChannelBinding {
* for when it is received.
*/
@JsonProperty("correlationData")
private Object correlationData;
private Schema correlationData;

/**
* String describing the content type of the message payload. This should not conflict with the contentType field
Expand Down Expand Up @@ -67,29 +66,13 @@ public static MQTTMessageBindingBuilder builder() {
}

public static class CustomMQTTMessageBinding extends MQTTMessageBindingBuilder {
private Object correlationData;
private Object responseTopic;

public MQTTMessageBindingBuilder correlationData(Schema schema) {
this.correlationData = schema;
return this;
}

public MQTTMessageBindingBuilder correlationData(Reference reference) {
this.correlationData = reference;
return this;
}

public MQTTMessageBindingBuilder responseTopic(Schema schema) {
this.responseTopic = schema;
return this;
}

public MQTTMessageBindingBuilder responseTopic(Reference reference) {
this.responseTopic = reference;
return this;
}

public MQTTMessageBindingBuilder responseTopic(String uriString) {
this.responseTopic = uriString;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import io.github.stavshamir.springwolf.asyncapi.v3.bindings.ChannelBinding;
import io.github.stavshamir.springwolf.asyncapi.v3.model.Reference;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.Schema;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
Expand Down Expand Up @@ -76,10 +75,5 @@ public MQTTOperationBindingBuilder messageExpiryInterval(Schema schema) {
this.messageExpiryInterval = schema;
return this;
}

public MQTTOperationBindingBuilder messageExpiryInterval(Reference reference) {
this.messageExpiryInterval = reference;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import io.github.stavshamir.springwolf.asyncapi.v3.bindings.ServerBinding;
import io.github.stavshamir.springwolf.asyncapi.v3.model.Reference;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -87,11 +86,6 @@ public MQTTServerBindingBuilder sessionExpiryInterval(Schema schema) {
return this;
}

public MQTTServerBindingBuilder sessionExpiryInterval(Reference reference) {
this.sessionExpiryInterval = reference;
return this;
}

public MQTTServerBindingBuilder maximumPacketSize(Integer integer) {
this.maximumPacketSize = integer;
return this;
Expand All @@ -101,10 +95,5 @@ public MQTTServerBindingBuilder maximumPacketSize(Schema schema) {
this.maximumPacketSize = schema;
return this;
}

public MQTTServerBindingBuilder maximumPacketSize(Reference reference) {
this.maximumPacketSize = reference;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.github.stavshamir.springwolf.asyncapi.v3.jackson.model.channel.message.MessageHeadersSerializer;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.MultiFormatSchema;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.Schema;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.SchemaObject;
import lombok.Getter;

@Getter
@JsonSerialize(using = MessageHeadersSerializer.class)
public class MessageHeaders {
private MultiFormatSchema multiFormatSchema;
private Schema schema;
private SchemaObject schema;
private MessageReference reference;

private MessageHeaders(MultiFormatSchema multiFormatSchema) {
this.multiFormatSchema = multiFormatSchema;
}

private MessageHeaders(Schema schema) {
private MessageHeaders(SchemaObject schema) {
this.schema = schema;
}

Expand All @@ -30,7 +30,7 @@ public static MessageHeaders of(MultiFormatSchema multiFormatSchema) {
return new MessageHeaders(multiFormatSchema);
}

public static MessageHeaders of(Schema schema) {
public static MessageHeaders of(SchemaObject schema) {
return new MessageHeaders(schema);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import io.github.stavshamir.springwolf.asyncapi.v3.jackson.model.channel.message.MessagePayloadSerializer;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.MultiFormatSchema;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.Schema;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.SchemaObject;
import lombok.Getter;

@Getter
@JsonSerialize(using = MessagePayloadSerializer.class)
public class MessagePayload {
private MultiFormatSchema multiFormatSchema;
private Schema schema;
private SchemaObject schema;
private MessageReference reference;

private MessagePayload(MultiFormatSchema multiFormatSchema) {
this.multiFormatSchema = multiFormatSchema;
}

private MessagePayload(Schema schema) {
private MessagePayload(SchemaObject schema) {
this.schema = schema;
}

Expand All @@ -30,7 +30,7 @@ public static MessagePayload of(MultiFormatSchema multiFormatSchema) {
return new MessagePayload(multiFormatSchema);
}

public static MessagePayload of(Schema schema) {
public static MessagePayload of(SchemaObject schema) {
return new MessagePayload(schema);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
import io.github.stavshamir.springwolf.asyncapi.v3.jackson.model.channel.message.ComponentSchemaSerializer;
import io.github.stavshamir.springwolf.asyncapi.v3.model.channel.message.MessageReference;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.MultiFormatSchema;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.Schema;
import io.github.stavshamir.springwolf.asyncapi.v3.model.schema.SchemaObject;
import lombok.Getter;

@Getter
@JsonSerialize(using = ComponentSchemaSerializer.class)
public class ComponentSchema {
private MultiFormatSchema multiFormatSchema;
private Schema schema;
private SchemaObject schema;
private MessageReference reference;

private ComponentSchema(MultiFormatSchema multiFormatSchema) {
this.multiFormatSchema = multiFormatSchema;
}

private ComponentSchema(Schema schema) {
private ComponentSchema(SchemaObject schema) {
this.schema = schema;
}

Expand All @@ -31,7 +31,7 @@ public static ComponentSchema of(MultiFormatSchema multiFormatSchema) {
return new ComponentSchema(multiFormatSchema);
}

public static ComponentSchema of(Schema schema) {
public static ComponentSchema of(SchemaObject schema) {
return new ComponentSchema(schema);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,115 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
package io.github.stavshamir.springwolf.asyncapi.v3.model.schema;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.github.stavshamir.springwolf.asyncapi.v3.model.ExtendableObject;
import io.github.stavshamir.springwolf.asyncapi.v3.model.ExternalDocumentation;
import io.github.stavshamir.springwolf.asyncapi.v3.model.Reference;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

/**
* The Schema Object allows the definition of input and output data types. These types can be objects, but also
* primitives and arrays. This object is a superset of the JSON Schema Specification Draft 07. The empty schema
* (which allows any instance to validate) MAY be represented by the boolean value true and a schema which allows no
* instance to validate MAY be represented by the boolean value false.
* </p>
* Further information about the properties can be found in JSON Schema Core and JSON Schema Validation. Unless stated
* otherwise, the property definitions follow the JSON Schema specification as referenced here.
*
* @see <a href="https://www.asyncapi.com/docs/reference/specification/v3.0.0#schemaObject">Schema Object</a>
* Represents a Schema. A Schema can be a SchemaObject or a SchemaReference.
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class Schema extends ExtendableObject implements Reference {
/**
* Adds support for polymorphism. The discriminator is the schema property name that is used to differentiate
* between other schema that inherit this schema. The property name used MUST be defined at this schema and it
* MUST be in the required property list. When used, the value MUST be the name of this schema or any schema that
* inherits it. See Composition and Inheritance for more details.
*/
@JsonProperty(value = "discriminator")
private String discriminator;

/**
* Additional external documentation for this schema.
*/
@JsonProperty(value = "externalDocs")
private ExternalDocumentation externalDocs;

/**
* Specifies that a schema is deprecated and SHOULD be transitioned out of usage. Default value is false.
*/
@JsonProperty(value = "deprecated")
private Boolean deprecated;

@JsonProperty(value = "title")
private String title;

@JsonProperty(value = "type")
private String type;

@JsonProperty(value = "properties")
private Map<String, Schema> properties;

/**
* <a href="https://spec.commonmark.org/">CommonMark syntax</a> can be used for rich text representation.
*/
@JsonProperty(value = "description")
private String description;

/**
* See Data Type Formats for further details. While relying on JSON Schema's defined formats, the AsyncAPI
* Specification offers a few additional predefined formats.
*/
@JsonProperty(value = "format")
private String format;

@JsonProperty(value = "maximum")
private Integer maximum;

@JsonProperty(value = "minimum")
private Integer minimum;

@JsonProperty("enum")
private List<String> enumValues;

@JsonProperty(value = "examples")
public List<Object> examples;

@JsonProperty(value = "additionalProperties")
public Schema additionalProperties;

@JsonProperty(value = "required")
public List<String> required;

@JsonProperty(value = "allOf")
public List<Schema> allOf;

@JsonProperty(value = "oneOf")
public List<Schema> oneOf;

@JsonProperty(value = "anyOf")
public List<Schema> anyOf;

@JsonProperty(value = "const")
public Object constValue;

@JsonIgnore
private String ref;

@Override
public String getRef() {
return ref;
}
}
public interface Schema {}
Loading

0 comments on commit 2a75dc4

Please sign in to comment.