![JAR search and dependency download from the Maven repository](/logo.png)
org.apache.juneau.oapi.OpenApiSerializerSession Maven / Gradle / Ivy
// ***************************************************************************************************************************
// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file *
// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file *
// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
// * with the License. You may obtain a copy of the License at *
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 *
// * *
// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an *
// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the *
// * specific language governing permissions and limitations under the License. *
// ***************************************************************************************************************************
package org.apache.juneau.oapi;
import static org.apache.juneau.common.internal.StringUtils.*;
import static org.apache.juneau.httppart.HttpPartCollectionFormat.*;
import static org.apache.juneau.httppart.HttpPartDataType.*;
import static org.apache.juneau.httppart.HttpPartFormat.*;
import java.io.IOException;
import java.lang.reflect.*;
import java.nio.charset.*;
import java.time.temporal.*;
import java.util.*;
import java.util.Date;
import java.util.function.*;
import org.apache.juneau.*;
import org.apache.juneau.collections.*;
import org.apache.juneau.common.internal.*;
import org.apache.juneau.httppart.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.serializer.*;
import org.apache.juneau.svl.*;
import org.apache.juneau.swap.*;
import org.apache.juneau.swaps.*;
import org.apache.juneau.uon.*;
/**
* Session object that lives for the duration of a single use of {@link OpenApiSerializer}.
*
* Notes:
* - This class is not thread safe and is typically discarded after one use.
*
*
* See Also:
* - OpenAPI Details
*
*/
public class OpenApiSerializerSession extends UonSerializerSession {
//-----------------------------------------------------------------------------------------------------------------
// Static
//-----------------------------------------------------------------------------------------------------------------
// Cache these for faster lookup
private static final BeanContext BC = BeanContext.DEFAULT;
private static final ClassMeta CM_ByteArray = BC.getClassMeta(byte[].class);
private static final ClassMeta CM_StringArray = BC.getClassMeta(String[].class);
private static final ClassMeta CM_Calendar = BC.getClassMeta(Calendar.class);
private static final ClassMeta CM_Long = BC.getClassMeta(Long.class);
private static final ClassMeta CM_Integer = BC.getClassMeta(Integer.class);
private static final ClassMeta CM_Double = BC.getClassMeta(Double.class);
private static final ClassMeta CM_Float = BC.getClassMeta(Float.class);
private static final ClassMeta CM_Boolean = BC.getClassMeta(Boolean.class);
private static final HttpPartSchema DEFAULT_SCHEMA = HttpPartSchema.DEFAULT;
/**
* Creates a new builder for this object.
*
* @param ctx The context creating this session.
* @return A new builder.
*/
public static Builder create(OpenApiSerializer ctx) {
return new Builder(ctx);
}
//-----------------------------------------------------------------------------------------------------------------
// Builder
//-----------------------------------------------------------------------------------------------------------------
/**
* Builder class.
*/
@FluentSetters
public static class Builder extends UonSerializerSession.Builder {
OpenApiSerializer ctx;
/**
* Constructor
*
* @param ctx The context creating this session.
*/
protected Builder(OpenApiSerializer ctx) {
super(ctx);
this.ctx = ctx;
}
@Override
public OpenApiSerializerSession build() {
return new OpenApiSerializerSession(this);
}
//
@Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
public Builder apply(Class type, Consumer apply) {
super.apply(type, apply);
return this;
}
@Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
public Builder debug(Boolean value) {
super.debug(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
public Builder properties(Map value) {
super.properties(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
public Builder property(String key, Object value) {
super.property(key, value);
return this;
}
@Override /* GENERATED - org.apache.juneau.ContextSession.Builder */
public Builder unmodifiable() {
super.unmodifiable();
return this;
}
@Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
public Builder locale(Locale value) {
super.locale(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
public Builder localeDefault(Locale value) {
super.localeDefault(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
public Builder mediaType(MediaType value) {
super.mediaType(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
public Builder mediaTypeDefault(MediaType value) {
super.mediaTypeDefault(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
public Builder timeZone(TimeZone value) {
super.timeZone(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.BeanSession.Builder */
public Builder timeZoneDefault(TimeZone value) {
super.timeZoneDefault(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
public Builder javaMethod(Method value) {
super.javaMethod(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
public Builder resolver(VarResolverSession value) {
super.resolver(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
public Builder schema(HttpPartSchema value) {
super.schema(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
public Builder schemaDefault(HttpPartSchema value) {
super.schemaDefault(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.serializer.SerializerSession.Builder */
public Builder uriContext(UriContext value) {
super.uriContext(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.serializer.WriterSerializerSession.Builder */
public Builder fileCharset(Charset value) {
super.fileCharset(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.serializer.WriterSerializerSession.Builder */
public Builder streamCharset(Charset value) {
super.streamCharset(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.serializer.WriterSerializerSession.Builder */
public Builder useWhitespace(Boolean value) {
super.useWhitespace(value);
return this;
}
@Override /* GENERATED - org.apache.juneau.uon.UonSerializerSession.Builder */
public Builder encoding(boolean value) {
super.encoding(value);
return this;
}
//
}
//-----------------------------------------------------------------------------------------------------------------
// Instance
//-----------------------------------------------------------------------------------------------------------------
private final OpenApiSerializer ctx;
/**
* Constructor.
*
* @param builder The builder for this object.
*/
protected OpenApiSerializerSession(Builder builder) {
super(builder.encoding(false));
ctx = builder.ctx;
}
@Override /* Serializer */
protected void doSerialize(SerializerPipe out, Object o) throws IOException, SerializeException {
try {
out.getWriter().write(serialize(HttpPartType.BODY, getSchema(), o));
} catch (SchemaValidationException e) {
throw new SerializeException(e);
}
}
@SuppressWarnings("rawtypes")
@Override /* PartSerializer */
public String serialize(HttpPartType partType, HttpPartSchema schema, Object value) throws SerializeException, SchemaValidationException {
ClassMeta> type = getClassMetaForObject(value);
if (type == null)
type = object();
// Swap if necessary
ObjectSwap swap = type.getSwap(this);
if (swap != null && ! type.isDateOrCalendarOrTemporal()) {
value = swap(swap, value);
type = swap.getSwapClassMeta(this);
// If the getSwapClass() method returns Object, we need to figure out
// the actual type now.
if (type.isObject())
type = getClassMetaForObject(value);
}
schema = ObjectUtils.firstNonNull(schema, DEFAULT_SCHEMA);
HttpPartDataType t = schema.getType(type);
HttpPartFormat f = schema.getFormat(type);
if (f == HttpPartFormat.NO_FORMAT)
f = ctx.getFormat();
HttpPartCollectionFormat cf = schema.getCollectionFormat();
if (cf == HttpPartCollectionFormat.NO_COLLECTION_FORMAT)
cf = ctx.getCollectionFormat();
String out = null;
schema.validateOutput(value, ctx.getBeanContext());
if (type.hasMutaterTo(schema.getParsedType()) || schema.getParsedType().hasMutaterFrom(type)) {
value = toType(value, schema.getParsedType());
type = schema.getParsedType();
}
if (type.isUri()) {
value = getUriResolver().resolve(value);
type = string();
}
if (value != null) {
if (t == STRING) {
if (f == BYTE) {
out = base64Encode(toType(value, CM_ByteArray));
} else if (f == BINARY) {
out = toHex(toType(value, CM_ByteArray));
} else if (f == BINARY_SPACED) {
out = toSpacedHex(toType(value, CM_ByteArray));
} else if (f == DATE) {
try {
if (value instanceof Calendar)
out = TemporalCalendarSwap.IsoDate.DEFAULT.swap(this, (Calendar)value);
else if (value instanceof Date)
out = TemporalDateSwap.IsoDate.DEFAULT.swap(this, (Date)value);
else if (value instanceof Temporal)
out = TemporalSwap.IsoDate.DEFAULT.swap(this, (Temporal)value);
else
out = value.toString();
} catch (Exception e) {
throw new SerializeException(e);
}
} else if (f == DATE_TIME) {
try {
if (value instanceof Calendar)
out = TemporalCalendarSwap.IsoInstant.DEFAULT.swap(this, (Calendar)value);
else if (value instanceof Date)
out = TemporalDateSwap.IsoInstant.DEFAULT.swap(this, (Date)value);
else if (value instanceof Temporal)
out = TemporalSwap.IsoInstant.DEFAULT.swap(this, (Temporal)value);
else
out = value.toString();
} catch (Exception e) {
throw new SerializeException(e);
}
} else if (f == HttpPartFormat.UON) {
out = super.serialize(partType, schema, value);
} else {
out = toType(value, string());
}
} else if (t == BOOLEAN) {
out = stringify(toType(value, CM_Boolean));
} else if (t == INTEGER) {
if (f == INT64)
out = stringify(toType(value, CM_Long));
else
out = stringify(toType(value, CM_Integer));
} else if (t == NUMBER) {
if (f == DOUBLE)
out = stringify(toType(value, CM_Double));
else
out = stringify(toType(value, CM_Float));
} else if (t == ARRAY) {
if (cf == HttpPartCollectionFormat.UONC)
out = super.serialize(partType, null, toList(partType, type, value, schema));
else {
HttpPartSchema items = schema.getItems();
ClassMeta> vt = getClassMetaForObject(value);
OapiStringBuilder sb = new OapiStringBuilder(cf);
if (type.isArray()) {
for (int i = 0; i < Array.getLength(value); i++)
sb.append(serialize(partType, items, Array.get(value, i)));
} else if (type.isCollection()) {
((Collection>)value).forEach(x -> sb.append(serialize(partType, items, x)));
} else if (vt.hasMutaterTo(String[].class)) {
String[] ss = toType(value, CM_StringArray);
for (String element : ss)
sb.append(serialize(partType, items, element));
} else {
throw new SerializeException("Input is not a valid array type: " + type);
}
out = sb.toString();
}
} else if (t == OBJECT) {
if (cf == HttpPartCollectionFormat.UONC) {
if (schema.hasProperties() && type.isMapOrBean())
value = toMap(partType, type, value, schema);
out = super.serialize(partType, null, value);
} else if (type.isBean()) {
OapiStringBuilder sb = new OapiStringBuilder(cf);
Predicate
© 2015 - 2025 Weber Informatics LLC | Privacy Policy