-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(specs): built-in ops accept also int (generated)
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
1 parent
b2827d2
commit 92a2a81
Showing
2 changed files
with
113 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
algoliasearch/src/main/java/com/algolia/model/search/BuiltInOperationValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |