1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.juneau.bean.openapi3;
18
19 import static org.apache.juneau.commons.utils.AssertionUtils.*;
20 import static org.apache.juneau.commons.utils.CollectionUtils.*;
21 import static org.apache.juneau.commons.utils.Utils.*;
22 import static org.apache.juneau.internal.ConverterUtils.*;
23
24 import java.util.*;
25
26 import org.apache.juneau.commons.collections.*;
27
28 /**
29 * A map of possible out-of-band callbacks related to the parent operation.
30 *
31 * <p>
32 * The Callback Object is a map of possible out-of-band callbacks related to the parent operation. Each value in the
33 * map is a Path Item Object that describes a set of requests that may be initiated by the API provider and the expected
34 * responses. The key value used to identify the callback object is an expression, evaluated at runtime, that identifies
35 * a URL to use for the callback operation.
36 *
37 * <h5 class='section'>OpenAPI Specification:</h5>
38 * <p>
39 * The Callback Object is composed of the following fields:
40 * <ul class='spaced-list'>
41 * <li><c>callbacks</c> (map of {@link PathItem}) - A map of possible out-of-band callbacks related to the parent operation
42 * </ul>
43 *
44 * <h5 class='section'>Example:</h5>
45 * <p class='bcode'>
46 * <jc>// Construct using SwaggerBuilder.</jc>
47 * Callback <jv>x</jv> = <jsm>callback</jsm>()
48 * .setCallbacks(<jsm>map</jsm>(<js>"myCallback"</js>, <jsm>pathItem</jsm>().setPost(<jsm>operation</jsm>().setSummary(<js>"Callback"</js>))));
49 *
50 * <jc>// Serialize using JsonSerializer.</jc>
51 * String <jv>json</jv> = Json.<jsm>from</jsm>(<jv>x</jv>);
52 *
53 * <jc>// Or just use toString() which does the same as above.</jc>
54 * <jv>json</jv> = <jv>x</jv>.toString();
55 * </p>
56 * <p class='bcode'>
57 * <jc>// Output</jc>
58 * {
59 * <js>"callbacks"</js>: {
60 * <js>"myCallback"</js>: {
61 * <js>"post"</js>: { <js>"summary"</js>: <js>"Callback"</js> }
62 * }
63 * }
64 * }
65 * </p>
66 *
67 * <h5 class='section'>See Also:</h5><ul>
68 * <li class='link'><a class="doclink" href="https://spec.openapis.org/oas/v3.0.0#callback-object">OpenAPI Specification > Callback Object</a>
69 * <li class='link'><a class="doclink" href="https://swagger.io/docs/specification/callbacks/">OpenAPI Callbacks</a>
70 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanOpenApi3">juneau-bean-openapi-v3</a>
71 * </ul>
72 */
73 public class Callback extends OpenApiElement {
74
75 private Map<String,PathItem> callbacks;
76
77 /**
78 * Default constructor.
79 */
80 public Callback() {}
81
82 /**
83 * Copy constructor.
84 *
85 * @param copyFrom The object to copy.
86 */
87 public Callback(Callback copyFrom) {
88 super(copyFrom);
89 this.callbacks = copyOf(copyFrom.callbacks);
90 }
91
92 /**
93 * Adds a callback.
94 *
95 * @param expression The callback expression. Must not be <jk>null</jk>.
96 * @param pathItem The path item for the callback. Must not be <jk>null</jk>.
97 * @return This object.
98 */
99 public Callback addCallback(String expression, PathItem pathItem) {
100 assertArgNotNull("expression", expression);
101 assertArgNotNull("pathItem", pathItem);
102 if (callbacks == null)
103 callbacks = new LinkedHashMap<>();
104 callbacks.put(expression, pathItem);
105 return this;
106 }
107
108 /**
109 * Creates a copy of this object.
110 *
111 * @return A copy of this object.
112 */
113 public Callback copy() {
114 return new Callback(this);
115 }
116
117 @Override /* Overridden from OpenApiElement */
118 public <T> T get(String property, Class<T> type) {
119 assertArgNotNull("property", property);
120 return switch (property) {
121 case "callbacks" -> toType(getCallbacks(), type);
122 default -> super.get(property, type);
123 };
124 }
125
126 /**
127 * Returns the callbacks map.
128 *
129 * @return The callbacks map.
130 */
131 public Map<String,PathItem> getCallbacks() { return callbacks; }
132
133 @Override /* Overridden from OpenApiElement */
134 public Set<String> keySet() {
135 // @formatter:off
136 var s = setb(String.class)
137 .addIf(nn(callbacks), "callbacks")
138 .build();
139 // @formatter:on
140 return new MultiSet<>(s, super.keySet());
141 }
142
143 @Override /* Overridden from OpenApiElement */
144 public Callback set(String property, Object value) {
145 assertArgNotNull("property", property);
146 return switch (property) {
147 case "callbacks" -> setCallbacks(toMapBuilder(value, String.class, PathItem.class).sparse().build());
148 default -> {
149 super.set(property, value);
150 yield this;
151 }
152 };
153 }
154
155 /**
156 * Sets the callbacks map.
157 *
158 * @param value The new value for this property.
159 * @return This object.
160 */
161 public Callback setCallbacks(Map<String,PathItem> value) {
162 callbacks = value;
163 return this;
164 }
165
166 @Override /* Overridden from OpenApiElement */
167 public Callback strict() {
168 super.strict();
169 return this;
170 }
171
172 @Override /* Overridden from OpenApiElement */
173 public Callback strict(Object value) {
174 super.strict(value);
175 return this;
176 }
177 }