001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.dto.swagger; 014 015import static org.apache.juneau.internal.BeanPropertyUtils.*; 016 017import java.util.*; 018 019import org.apache.juneau.annotation.*; 020import org.apache.juneau.json.*; 021 022/** 023 * Root class for all Swagger beans. 024 * 025 * <h5 class='section'>See Also:</h5> 026 * <ul class='doctree'> 027 * <li class='link'><a class='doclink' href='../../../../../overview-summary.html#juneau-dto.Swagger'>Overview > juneau-dto > Swagger</a> 028 * </ul> 029 */ 030public abstract class SwaggerElement { 031 032 private boolean strict; 033 private Map<String,Object> extra; 034 035 /** 036 * Returns <jk>true</jk> if contents should be validated per the Swagger spec. 037 * 038 * @return <jk>true</jk> if contents should be validated per the Swagger spec. 039 */ 040 protected boolean isStrict() { 041 return strict; 042 } 043 044 /** 045 * Sets strict mode on this bean. 046 * 047 * @return This object (for method chaining). 048 */ 049 protected SwaggerElement strict() { 050 strict = true; 051 return this; 052 } 053 054 /** 055 * Sets strict mode on this bean. 056 * 057 * @param value 058 * The new value for this property. 059 * <br>Non-boolean values will be converted to boolean using <code>Boolean.<jsm>valueOf</jsm>(value.toString())</code>. 060 * <br>Can be <jk>null</jk> (interpreted as <jk>false</jk>). 061 * @return This object (for method chaining). 062 */ 063 protected SwaggerElement strict(Object value) { 064 strict = value == null ? false : toBoolean(value); 065 return this; 066 } 067 068 /** 069 * The map used to store 'extra' properties on the swagger element. 070 * 071 * <p> 072 * For example, the <js>"$ref"</js> field is not a part of the Swagger doc, but is often 073 * found since it is a part of the JSON-Schema spec. 074 * <br>This map allows you to store such properties. 075 * 076 * <p> 077 * This map is lazy-created once this method is called. 078 * 079 * @return 080 * The extra properties map. 081 * <br>It's an instance of {@link LinkedHashMap}. 082 */ 083 @BeanProperty("*") 084 public Map<String,Object> getExtraProperties() { 085 if (extra == null) 086 extra = new LinkedHashMap<>(); 087 return extra; 088 } 089 090 /** 091 * Generic property getter. 092 * 093 * <p> 094 * Can be used to retrieve non-standard Swagger fields such as <js>"$ref"</js>. 095 * 096 * @param property The property name to retrieve. 097 * @param type The datatype to cast the value to. 098 * @return The property value, or <jk>null</jk> if the property does not exist or is not set. 099 */ 100 public <T> T get(String property, Class<T> type) { 101 if (property == null) 102 return null; 103 switch (property) { 104 case "strict": return toType(isStrict(), type); 105 default: return toType(getExtraProperties().get(property), type); 106 } 107 }; 108 109 /** 110 * Generic property setter. 111 * 112 * <p> 113 * Can be used to set non-standard Swagger fields such as <js>"$ref"</js>. 114 * 115 * @param property The property name to set. 116 * @param value The new value for the property. 117 * @return This object (for method chaining). 118 */ 119 public SwaggerElement set(String property, Object value) { 120 if (property == null) 121 return this; 122 switch (property) { 123 case "strict": return strict(value); 124 default: 125 getExtraProperties().put(property, value); 126 return this; 127 } 128 } 129 130 @Override /* Object */ 131 public String toString() { 132 return JsonSerializer.DEFAULT.toString(this); 133 } 134}