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