Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.JsonSerializer;
import com.azure.core.util.serializer.JsonSerializerProviders;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -20,19 +19,15 @@
/**
* Represents a JSON Patch document.
*/
//@JsonSerialize(using = JsonPatchDocumentSerializer.class)
public class JsonPatchDocument {
@JsonSerialize(using = JsonPatchDocumentSerializer.class)
public final class JsonPatchDocument {
private static final Object SERIALIZER_INSTANTIATION_SYNCHRONIZER = new Object();
private static volatile JsonSerializer defaultSerializer;

@JsonIgnore
private final ClientLogger logger = new ClientLogger(JsonPatchDocument.class);

@JsonValue
private final List<JsonPatchOperation> operations;

@JsonIgnore
private final JsonSerializer serializer;
private final List<JsonPatchOperation> operations;

/**
* Creates a new JSON Patch document.
Expand All @@ -53,7 +48,14 @@ public JsonPatchDocument(JsonSerializer serializer) {
this.serializer = serializer;
}

List<JsonPatchOperation> getOperations() {
/**
* Gets a representation of the {@link JsonPatchOperation JSON patch operations} in this JSON patch document.
* <p>
* Modifications to the returned list won't mutate the operations in the document.
*
* @return The JSON patch operations in this JSON patch document.
*/
public List<JsonPatchOperation> getOperations() {
return new ArrayList<>(operations);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Handles serialization of a {@link JsonPatchDocument}.
*/
class JsonPatchDocumentSerializer extends JsonSerializer<JsonPatchDocument> {
final class JsonPatchDocumentSerializer extends JsonSerializer<JsonPatchDocument> {
@Override
public void serialize(JsonPatchDocument value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* Represents a JSON Patch operation.
*/
@JsonSerialize(using = JsonPatchOperationSerializer.class)
final class JsonPatchOperation {
public final class JsonPatchOperation {
private final JsonPatchOperationKind op;
private final String from;
private final String path;
Expand Down Expand Up @@ -41,7 +41,7 @@ final class JsonPatchOperation {
*
* @return The kind of operation.
*/
JsonPatchOperationKind getOp() {
public JsonPatchOperationKind getOp() {
return op;
}

Expand All @@ -50,7 +50,7 @@ JsonPatchOperationKind getOp() {
*
* @return The operation from target path.
*/
String getFrom() {
public String getFrom() {
return from;
}

Expand All @@ -59,16 +59,18 @@ String getFrom() {
*
* @return The operation target path.
*/
String getPath() {
public String getPath() {
return path;
}

/**
* Gets the operation value.
* <p>
* If the operation doesn't take a value {@link Option#uninitialized()} will be returned.
*
* @return The operation value.
*/
Option<String> getValue() {
public Option<String> getValue() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Represents the JSON Patch operation kind.
*/
enum JsonPatchOperationKind {
public enum JsonPatchOperationKind {
/**
* Add operation.
*/
Expand Down Expand Up @@ -45,6 +45,11 @@ enum JsonPatchOperationKind {
this.op = op;
}

/**
* Gets the string representation of the JSON patch operation kind.
*
* @return The string representation of the JSON patch operation kind.
*/
@JsonValue
public String toString() {
return op;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* Handles serialization of a {@link JsonPatchOperation}.
*/
class JsonPatchOperationSerializer extends JsonSerializer<JsonPatchOperation> {
final class JsonPatchOperationSerializer extends JsonSerializer<JsonPatchOperation> {
@Override
public void serialize(JsonPatchOperation value, JsonGenerator gen, SerializerProvider serializers)
throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public void jsonifyDocument(JsonPatchDocument document, String expected) throws
assertEquals(expected, SERIALIZER.serialize(document, SerializerEncoding.JSON).replace(" ", ""));
}

@ParameterizedTest
@MethodSource("formattingSupplier")
public void jsonifyOperations(JsonPatchDocument document, String expected) throws IOException {
assertEquals(expected, SERIALIZER.serialize(document.getOperations(), SerializerEncoding.JSON)
.replace(" ", ""));
}

private static Stream<Arguments> formattingSupplier() {
JsonPatchDocument complexDocument = newDocument()
.appendTest("/a/b/c", "foo")
Expand Down