Skip to content

Commit

Permalink
fix(specs): built-in ops accept also int (generated)
Browse files Browse the repository at this point in the history
algolia/api-clients-automation#3450

Co-authored-by: algolia-bot <accounts+algolia-api-client-bot@algolia.com>
Co-authored-by: Kai Welke <kai.welke@algolia.com>
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
  • Loading branch information
3 people committed Jul 31, 2024
1 parent b2827d2 commit 92a2a81
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class BuiltInOperation implements AttributeToUpdate {
private BuiltInOperationType operation;

@JsonProperty("value")
private String value;
private BuiltInOperationValue value;

public BuiltInOperation setOperation(BuiltInOperationType operation) {
this.operation = operation;
Expand All @@ -28,17 +28,14 @@ public BuiltInOperationType getOperation() {
return operation;
}

public BuiltInOperation setValue(String value) {
public BuiltInOperation setValue(BuiltInOperationValue value) {
this.value = value;
return this;
}

/**
* Value that corresponds to the operation, for example an `Increment` or `Decrement` step, or an
* `Add` or `Remove` value.
*/
/** Get value */
@javax.annotation.Nonnull
public String getValue() {
public BuiltInOperationValue getValue() {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost
// - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT.

package com.algolia.model.search;

import com.algolia.exceptions.AlgoliaRuntimeException;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.*;
import java.io.IOException;
import java.util.logging.Logger;

/** BuiltInOperationValue */
@JsonDeserialize(using = BuiltInOperationValue.Deserializer.class)
public interface BuiltInOperationValue {
// BuiltInOperationValue as String wrapper.
static BuiltInOperationValue of(String value) {
return new StringWrapper(value);
}

// BuiltInOperationValue as Integer wrapper.
static BuiltInOperationValue of(Integer value) {
return new IntegerWrapper(value);
}

// BuiltInOperationValue as String wrapper.
@JsonSerialize(using = StringWrapper.Serializer.class)
class StringWrapper implements BuiltInOperationValue {

private final String value;

StringWrapper(String value) {
this.value = value;
}

public String getValue() {
return value;
}

static class Serializer extends JsonSerializer<StringWrapper> {

@Override
public void serialize(StringWrapper value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeObject(value.getValue());
}
}
}

// BuiltInOperationValue as Integer wrapper.
@JsonSerialize(using = IntegerWrapper.Serializer.class)
class IntegerWrapper implements BuiltInOperationValue {

private final Integer value;

IntegerWrapper(Integer value) {
this.value = value;
}

public Integer getValue() {
return value;
}

static class Serializer extends JsonSerializer<IntegerWrapper> {

@Override
public void serialize(IntegerWrapper value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeObject(value.getValue());
}
}
}

class Deserializer extends JsonDeserializer<BuiltInOperationValue> {

private static final Logger LOGGER = Logger.getLogger(Deserializer.class.getName());

@Override
public BuiltInOperationValue deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
JsonNode tree = jp.readValueAsTree();
// deserialize String
if (tree.isTextual()) {
try (JsonParser parser = tree.traverse(jp.getCodec())) {
String value = parser.readValueAs(String.class);
return new BuiltInOperationValue.StringWrapper(value);
} catch (Exception e) {
// deserialization failed, continue
LOGGER.finest("Failed to deserialize oneOf String (error: " + e.getMessage() + ") (type: String)");
}
}
// deserialize Integer
if (tree.isInt()) {
try (JsonParser parser = tree.traverse(jp.getCodec())) {
Integer value = parser.readValueAs(Integer.class);
return new BuiltInOperationValue.IntegerWrapper(value);
} catch (Exception e) {
// deserialization failed, continue
LOGGER.finest("Failed to deserialize oneOf Integer (error: " + e.getMessage() + ") (type: Integer)");
}
}
throw new AlgoliaRuntimeException(String.format("Failed to deserialize json element: %s", tree));
}

/** Handle deserialization of the 'null' value. */
@Override
public BuiltInOperationValue getNullValue(DeserializationContext ctxt) throws JsonMappingException {
throw new JsonMappingException(ctxt.getParser(), "BuiltInOperationValue cannot be null");
}
}
}

0 comments on commit 92a2a81

Please sign in to comment.