This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
TypeDeclaration.java
399 lines (361 loc) · 14.1 KB
/
TypeDeclaration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
* Copyright 2013 (c) MuleSoft, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.raml.v2.internal.impl.commons.model.type;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import org.apache.ws.commons.schema.XmlSchema;
import org.raml.v2.api.loader.ResourceLoader;
import org.raml.v2.internal.impl.commons.model.Annotable;
import org.raml.v2.internal.impl.commons.model.RamlValidationResult;
import org.raml.v2.internal.impl.commons.model.factory.TypeDeclarationModelFactory;
import org.raml.v2.internal.impl.commons.nodes.ExternalSchemaTypeExpressionNode;
import org.raml.v2.internal.impl.commons.nodes.TypeDeclarationNode;
import org.raml.v2.internal.impl.commons.nodes.TypeExpressionNode;
import org.raml.v2.internal.impl.commons.type.AbstractExternalType;
import org.raml.v2.internal.impl.commons.type.ResolvedType;
import org.raml.v2.internal.impl.commons.type.SchemaBasedResolvedType;
import org.raml.v2.internal.impl.v10.nodes.NamedTypeExpressionNode;
import org.raml.v2.internal.impl.v10.nodes.PropertyNode;
import org.raml.v2.internal.impl.v10.phase.ExampleValidationPhase;
import org.raml.v2.internal.impl.v10.type.AnyResolvedType;
import org.raml.v2.internal.impl.v10.type.TypeToJsonSchemaVisitor;
import org.raml.v2.internal.impl.v10.type.TypeToXmlSchemaVisitor;
import org.raml.v2.internal.impl.v10.type.XmlFacetsCapableType;
import org.raml.v2.internal.utils.JSonDumper;
import org.raml.yagi.framework.nodes.ArrayNode;
import org.raml.yagi.framework.nodes.ErrorNode;
import org.raml.yagi.framework.nodes.KeyValueNode;
import org.raml.yagi.framework.nodes.KeyValueNodeImpl;
import org.raml.yagi.framework.nodes.Node;
import org.raml.yagi.framework.nodes.SimpleTypeNode;
import org.raml.yagi.framework.nodes.StringNode;
import org.raml.yagi.framework.nodes.StringNodeImpl;
import org.raml.yagi.framework.nodes.snakeyaml.SYArrayNode;
import org.raml.yagi.framework.nodes.snakeyaml.SYObjectNode;
import org.raml.yagi.framework.util.NodeUtils;
import javax.annotation.Nullable;
import javax.json.JsonObject;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static java.util.Collections.singletonList;
import static org.raml.v2.internal.impl.commons.RamlVersion.RAML_10;
import static org.raml.v2.internal.utils.RamlNodeUtils.getVersion;
import static org.raml.yagi.framework.nodes.DefaultPosition.ARTIFICIAL_NODE;
import static org.raml.yagi.framework.util.NodeSelector.selectFrom;
import static org.raml.yagi.framework.util.NodeSelector.selectType;
public abstract class TypeDeclaration<T extends ResolvedType> extends Annotable<KeyValueNode>
{
private static final List<String> GLOBAL_NAMED_TYPE_DECLARATION_NODE_NAMES = Arrays.asList("types", "annotationTypes", "baseUriParameters");
private static final List<String> LOCAL_NAMED_TYPE_DECLARATION_NODE_NAMES = Arrays.asList("headers", "queryParameters", "queryString", "uriParameters");
private T resolvedType;
public TypeDeclaration(KeyValueNode node, T resolvedType)
{
super(node);
this.resolvedType = resolvedType;
}
public T getResolvedType()
{
return resolvedType;
}
@Override
public Node getNode()
{
return node.getValue();
}
public String name()
{
if (node instanceof PropertyNode)
{
return ((PropertyNode) node).getName();
}
String keyValue = ((StringNode) node.getKey()).getValue();
Node parentTypeNode = node.getValue();
if (node.getParent() instanceof TypeDeclarationNode && parentTypeNode instanceof TypeExpressionNode)
{
// getting name of parent type
if (parentTypeNode instanceof ExternalSchemaTypeExpressionNode)
{
return null;
}
return ((TypeExpressionNode) parentTypeNode).getTypeExpressionText();
}
return keyValue;
}
@Nullable
public String type()
{
Node typeNode = getTypeNode();
return typeNode != null ? getTypeExpression(typeNode) : null;
}
private String getTypeExpression(Node typeNode)
{
if (typeNode instanceof TypeExpressionNode)
{
return ((TypeExpressionNode) typeNode).getTypeExpressionText();
}
if (typeNode.getSource() instanceof StringNode)
{
return ((StringNode) typeNode.getSource()).getValue();
}
return null;
}
public List<TypeDeclaration<?>> parentTypes()
{
List<TypeDeclaration<?>> result = new ArrayList<>();
Node typeNode = getTypeNode();
if (typeNode instanceof ArrayNode)
{
List<Node> children = typeNode.getChildren();
for (Node child : children)
{
result.add(toTypeDeclaration(((NamedTypeExpressionNode) child).getRefNode()));
}
}
else if (typeNode instanceof TypeExpressionNode)
{
ResolvedType type = ((TypeExpressionNode) typeNode).generateDefinition();
if (type != null)
{
result.add(toTypeDeclaration(type));
}
}
return result;
}
private TypeDeclaration<?> toTypeDeclaration(ResolvedType type)
{
return new TypeDeclarationModelFactory().create(type);
}
private TypeDeclaration<?> toTypeDeclaration(Node typeNode)
{
return new TypeDeclarationModelFactory().create(typeNode);
}
public String schemaContent()
{
if (node.getValue() instanceof TypeDeclarationNode)
{
final TypeDeclarationNode value = (TypeDeclarationNode) node.getValue();
final List<TypeExpressionNode> baseTypes = value.getBaseTypes();
if (!baseTypes.isEmpty())
{
final ResolvedType resolvedType = baseTypes.get(0).generateDefinition();
if (resolvedType instanceof SchemaBasedResolvedType)
{
return ((SchemaBasedResolvedType) resolvedType).getSchemaValue();
}
}
}
return null;
}
public List<RamlValidationResult> validate(String payload)
{
TypeDeclarationNode typeDeclarationNode;
if (getNode() instanceof TypeDeclarationNode)
{
typeDeclarationNode = (TypeDeclarationNode) getNode();
}
else
{
// if union or array of scalar types, TypeDeclarationNode is not generated
typeDeclarationNode = new TypeDeclarationNode();
typeDeclarationNode.addChild(new KeyValueNodeImpl(new StringNodeImpl("type"), getResolvedType().getTypeExpressionNode()));
}
final ResourceLoader resourceLoader = typeDeclarationNode.getStartPosition().createResourceLoader();
final ExampleValidationPhase exampleValidationPhase = new ExampleValidationPhase(resourceLoader);
final Node validate = exampleValidationPhase.validate(typeDeclarationNode, payload);
if (NodeUtils.isErrorResult(validate))
{
ErrorNode error = validate instanceof ErrorNode ? (ErrorNode) validate : validate.findDescendantsWith(ErrorNode.class).get(0);
return singletonList(new RamlValidationResult(error));
}
else
{
return Collections.emptyList();
}
}
public Boolean required()
{
if (node instanceof PropertyNode)
{
return ((PropertyNode) node).isRequired();
}
else
{
// in Raml 1.0 parameters are required by default
// in Raml 0.8 parameters are optional by default except for uri parameters
if (getVersion(getNode()) == RAML_10 || isUriParameter(getNode()))
{
return selectType("required", getNode(), true);
}
return selectType("required", getNode(), false);
}
}
private boolean isUriParameter(Node node)
{
Node ancestor = selectFrom("../../../../uriParameters", node);
return ancestor != null;
}
public String defaultValue()
{
Node result = selectFrom("default", getNode());
if (result != null)
{
if (result instanceof SimpleTypeNode)
return ((SimpleTypeNode) result).getValue().toString();
if (result instanceof SYObjectNode || result instanceof SYArrayNode)
return JSonDumper.dump(result);
}
return null;
}
public String toXmlSchema()
{
if (getResolvedType() instanceof SchemaBasedResolvedType || getResolvedType() instanceof AnyResolvedType || getResolvedType() == null)
{
return null;
}
final TypeToXmlSchemaVisitor typeToXmlSchemaVisitor = new TypeToXmlSchemaVisitor();
typeToXmlSchemaVisitor.transform(rootElementName(), getResolvedType());
final XmlSchema schema = typeToXmlSchemaVisitor.getSchema();
final StringWriter writer = new StringWriter();
schema.write(writer);
return writer.toString();
}
public String toJsonSchema()
{
if (getResolvedType() instanceof SchemaBasedResolvedType || getResolvedType() instanceof AnyResolvedType || getResolvedType() == null)
{
return null;
}
final TypeToJsonSchemaVisitor typeToJsonSchemaVisitor = new TypeToJsonSchemaVisitor();
JsonObject jsonSchema = typeToJsonSchemaVisitor.transform(this.getResolvedType());
return jsonSchema.toString();
}
public String rootElementName()
{
if (resolvedType instanceof XmlFacetsCapableType)
{
// an XML name facet overrides any other possibility for the root XML element
String xmlName = ((XmlFacetsCapableType) resolvedType).getXmlFacets().getName();
if (xmlName != null)
{
return xmlName;
}
}
if (!(node instanceof PropertyNode) && !isGlobalNamedTypeDeclarationNode() && !isLocalNamedTypeDeclarationNode())
{
// type reference
final Node value = node.getValue();
if (value.get("properties") != null || value.get("facets") != null || !(getTypeNode() instanceof NamedTypeExpressionNode))
{
// type is extending somehow supertype or inline declaration
return "root";
}
else
{
// type is just referencing an existing one
return ((NamedTypeExpressionNode) getTypeNode()).getRefName();
}
}
else
{
// type declaration
return name();
}
}
public String internalFragment()
{
ResolvedType resolvedType = getResolvedType();
if (resolvedType instanceof AbstractExternalType)
{
return ((AbstractExternalType) resolvedType).getInternalFragment();
}
else
{
return null;
}
}
public String schemaPath()
{
ResolvedType resolvedType = getResolvedType();
if (resolvedType instanceof AbstractExternalType)
{
if (!((AbstractExternalType) resolvedType).getSchemaPath().equalsIgnoreCase(ARTIFICIAL_NODE))
{
return ((AbstractExternalType) resolvedType).getSchemaPath();
}
return null;
}
else
{
return null;
}
}
@Nullable
private Node getTypeNode()
{
final Node value = node.getValue();
final Node type = value.get("type");
if (type != null)
{
return type;
}
else
{
return value.get("schema");
}
}
/**
* True if the node is a global named type declaration as opposed to an inline/anonymous declaration. This is computed based on the parent of the current type declaration node, i.e.
* if its parent is one of the global named type declaration nodes possible.
*
* @return <code>true</code> if the node is a global named type declaration as opposed to an inline/anonymous declaration, <code>false</code> otherwise
*/
private boolean isGlobalNamedTypeDeclarationNode()
{
final Node parent = node.getParent();
final Node rootNode = NodeUtils.getContextNode(node);
return Iterables.any(GLOBAL_NAMED_TYPE_DECLARATION_NODE_NAMES, new Predicate<String>()
{
@Override
public boolean apply(final String name)
{
return parent == rootNode.get(name);
}
});
}
/**
* True if the node is a local named type declaration as opposed to an inline/anonymous declaration. This is computed based on the parent of the current type declaration node, i.e.
* if its parent is one of the local named type declaration nodes possible.
*
* @return <code>true</code> if the node is a local named type declaration as opposed to an inline/anonymous declaration, <code>false</code> otherwise
*/
private boolean isLocalNamedTypeDeclarationNode()
{
final Node parent = node.getParent();
// tries to get the grandparent of the current node to retrieve the same node based on the known local named declaration node names
final Node baseNode = parent.getParent().getParent();
return baseNode != null && Iterables.any(LOCAL_NAMED_TYPE_DECLARATION_NODE_NAMES, new Predicate<String>()
{
@Override
public boolean apply(final String name)
{
return parent == baseNode.get(name);
}
});
}
}