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