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.CollectionUtils.*; 016import static org.apache.juneau.internal.ConverterUtils.*; 017 018import java.util.*; 019 020import org.apache.juneau.annotation.*; 021import org.apache.juneau.collections.*; 022import org.apache.juneau.internal.*; 023import org.apache.juneau.json.*; 024 025/** 026 * Root class for all Swagger beans. 027 * 028 * <h5 class='section'>See Also:</h5><ul> 029 * <li class='link'><a class="doclink" href="../../../../../index.html#jrs.Swagger">Overview > juneau-rest-server > Swagger</a> 030 * </ul> 031 */ 032@FluentSetters 033public abstract class SwaggerElement { 034 035 private boolean strict; 036 private Map<String,Object> extra; 037 038 SwaggerElement() {} 039 040 SwaggerElement(SwaggerElement copyFrom) { 041 this.strict = copyFrom.strict; 042 this.extra = copyOf(copyFrom.extra); 043 } 044 045 /** 046 * Returns <jk>true</jk> if contents should be validated per the Swagger spec. 047 * 048 * @return <jk>true</jk> if contents should be validated per the Swagger spec. 049 */ 050 protected boolean isStrict() { 051 return strict; 052 } 053 054 /** 055 * Sets strict mode on this bean. 056 * 057 * @return This object. 058 */ 059 protected SwaggerElement strict() { 060 strict = true; 061 return this; 062 } 063 064 /** 065 * Sets strict mode on this bean. 066 * 067 * @param value 068 * The new value for this property. 069 * <br>Non-boolean values will be converted to boolean using <code>Boolean.<jsm>valueOf</jsm>(value.toString())</code>. 070 * <br>Can be <jk>null</jk> (interpreted as <jk>false</jk>). 071 * @return This object. 072 */ 073 protected SwaggerElement strict(Object value) { 074 strict = value == null ? false : toBoolean(value); 075 return this; 076 } 077 078 /** 079 * Generic property getter. 080 * 081 * <p> 082 * Can be used to retrieve non-standard Swagger fields such as <js>"$ref"</js>. 083 * 084 * @param <T> The datatype to cast the value to. 085 * @param property The property name to retrieve. 086 * @param type The datatype to cast the value to. 087 * @return The property value, or <jk>null</jk> if the property does not exist or is not set. 088 */ 089 public <T> T get(String property, Class<T> type) { 090 if (property == null) 091 return null; 092 switch (property) { 093 case "strict": return toType(isStrict(), type); 094 default: return toType(get(property), type); 095 } 096 } 097 098 /** 099 * Generic property getter. 100 * 101 * <p> 102 * Can be used to retrieve non-standard Swagger fields such as <js>"$ref"</js>. 103 * 104 * @param property The property name to retrieve. 105 * @return The property value, or <jk>null</jk> if the property does not exist or is not set. 106 */ 107 @Beanp("*") 108 public Object get(String property) { 109 if (property == null || extra == null) 110 return null; 111 return extra.get(property); 112 } 113 114 /** 115 * Generic property setter. 116 * 117 * <p> 118 * Can be used to set non-standard Swagger fields such as <js>"$ref"</js>. 119 * 120 * @param property The property name to set. 121 * @param value The new value for the property. 122 * @return This object. 123 */ 124 @Beanp("*") 125 public SwaggerElement set(String property, Object value) { 126 if (property == null) 127 return this; 128 switch (property) { 129 case "strict": return strict(value); 130 default: 131 if (extra == null) 132 extra = map(); 133 extra.put(property, value); 134 return this; 135 } 136 } 137 138 /** 139 * Generic property keyset. 140 * 141 * @return 142 * All the non-standard keys on this element. 143 * <br>Never <jk>null</jk>. 144 */ 145 @Beanp("*") 146 public Set<String> extraKeys() { 147 return extra == null ? Collections.emptySet() : extra.keySet(); 148 } 149 150 /** 151 * Returns all the keys on this element. 152 * 153 * @return 154 * All the keys on this element. 155 * <br>Never <jk>null</jk>. 156 */ 157 public Set<String> keySet() { 158 Set<String> s = setBuilder(String.class) 159 .addIf(strict, "strict") 160 .build(); 161 s.addAll(extraKeys()); 162 return s; 163 } 164 165 /** 166 * Returns a copy of this swagger element as a modifiable map. 167 * 168 * <p> 169 * Each call produces a new map. 170 * 171 * @return A map containing all the values in this swagger element. 172 */ 173 public JsonMap asMap() { 174 JsonMap m = new JsonMap(); 175 keySet().forEach(x -> m.put(x, get(x, Object.class))); 176 return m; 177 } 178 179 // <FluentSetters> 180 181 // </FluentSetters> 182 183 @Override /* Object */ 184 public String toString() { 185 return JsonSerializer.DEFAULT.toString(this); 186 } 187}