All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.juneau.dto.openapi3.OpenApiElement 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.dto.openapi3;

import static org.apache.juneau.internal.CollectionUtils.*;
import static org.apache.juneau.internal.ConverterUtils.*;

import org.apache.juneau.annotation.*;
import org.apache.juneau.collections.*;
import org.apache.juneau.internal.*;
import org.apache.juneau.json.JsonSerializer;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * Root class for all Swagger beans.
 */
@FluentSetters
public abstract class OpenApiElement {

	private boolean strict;
	private Map extra;

	OpenApiElement() {}

	OpenApiElement(OpenApiElement copyFrom) {
		this.strict = copyFrom.strict;
		this.extra = copyFrom.extra == null ? null : new LinkedHashMap<>(copyFrom.extra);
	}

	/**
	 * Returns true if contents should be validated per the Swagger spec.
	 *
	 * @return true if contents should be validated per the Swagger spec.
	 */
	protected boolean isStrict() {
		return strict;
	}

	/**
	 * Sets strict mode on this bean.
	 *
	 * @return This object
	 */
	protected OpenApiElement strict() {
		strict = true;
		return this;
	}

	/**
	 * Sets strict mode on this bean.
	 *
	 * @param value
	 * 	The new value for this property.
	 * 	
Non-boolean values will be converted to boolean using Boolean.valueOf(value.toString()). *
Can be null (interpreted as false). * @return This object */ protected OpenApiElement strict(Object value) { strict = value == null ? false : toBoolean(value); return this; } /** * Generic property getter. * *

* Can be used to retrieve non-standard Swagger fields such as "$ref". * * @param property The property name to retrieve. * @param type The datatype to cast the value to. * @param The datatype to cast the value to. * @return The property value, or null if the property does not exist or is not set. */ public T get(String property, Class type) { if (property == null) return null; switch (property) { case "strict": return toType(isStrict(), type); default: return toType(get(property), type); } } /** * Generic property getter. * *

* Can be used to retrieve non-standard Swagger fields such as "$ref". * * @param property The property name to retrieve. * @return The property value, or null if the property does not exist or is not set. */ @Beanp("*") public Object get(String property) { if (property == null || extra == null) return null; return extra.get(property); } /** * Generic property setter. * *

* Can be used to set non-standard Swagger fields such as "$ref". * * @param property The property name to set. * @param value The new value for the property. * @return This object */ @Beanp("*") public OpenApiElement set(String property, Object value) { if (property == null) return this; switch (property) { case "strict": return strict(value); default: if (extra == null) extra = new LinkedHashMap<>(); extra.put(property, value); return this; } } /** * Generic property keyset. * * @return * All the non-standard keys on this element. *
Never null. */ @Beanp("*") public Set extraKeys() { return extra == null ? Collections.emptySet() : extra.keySet(); } /** * Returns all the keys on this element. * * @return * All the keys on this element. *
Never null. */ public Set keySet() { Set s = setBuilder(String.class) .addIf(strict, "strict") .build(); s.addAll(extraKeys()); return s; } /** * Returns a copy of this swagger element as a modifiable map. * *

* Each call produces a new map. * * @return A map containing all the values in this swagger element. */ public JsonMap asMap() { JsonMap m = new JsonMap(); for (String s : keySet()) m.put(s, get(s, Object.class)); return m; } // // @Override /* Object */ public String toString() { return JsonSerializer.DEFAULT.toString(this); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy