/
YAMLUtil.java
196 lines (139 loc) · 6.41 KB
/
YAMLUtil.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
/**
* SPDX-FileCopyrightText: (c) 2000 Liferay, Inc. https://liferay.com
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
*/
package com.liferay.portal.tools.rest.builder.internal.yaml;
import com.liferay.portal.tools.rest.builder.internal.yaml.config.ConfigYAML;
import com.liferay.portal.tools.rest.builder.internal.yaml.config.Security;
import com.liferay.portal.tools.rest.builder.internal.yaml.exception.InvalidYAMLException;
import com.liferay.portal.tools.rest.builder.internal.yaml.exception.OpenAPIValidatorException;
import com.liferay.portal.tools.rest.builder.internal.yaml.openapi.Items;
import com.liferay.portal.tools.rest.builder.internal.yaml.openapi.OpenAPIYAML;
import com.liferay.portal.tools.rest.builder.internal.yaml.openapi.Parameter;
import com.liferay.portal.tools.rest.builder.internal.yaml.openapi.PathItem;
import com.liferay.portal.tools.rest.builder.internal.yaml.openapi.Schema;
import com.liferay.portal.tools.rest.builder.internal.yaml.openapi.XML;
import java.util.List;
import java.util.Map;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.error.MarkedYAMLException;
import org.yaml.snakeyaml.introspector.PropertyUtils;
import org.yaml.snakeyaml.representer.Representer;
/**
* @author Peter Shin
*/
public class YAMLUtil {
public static ConfigYAML loadConfigYAML(String yamlString) {
try {
return _YAML_CONFIG.loadAs(yamlString, ConfigYAML.class);
}
catch (MarkedYAMLException markedYAMLException) {
throw new InvalidYAMLException(markedYAMLException);
}
}
public static OpenAPIYAML loadOpenAPIYAML(String yamlString) {
try {
return _YAML_OPEN_API.loadAs(yamlString, OpenAPIYAML.class);
}
catch (MarkedYAMLException markedYAMLException) {
throw new InvalidYAMLException(markedYAMLException);
}
}
public static void validateOpenAPIYAML(String fileName, String yamlString)
throws OpenAPIValidatorException {
OpenAPIValidator.validate(fileName, yamlString, _YAML_OPEN_API);
}
private static final Yaml _YAML_CONFIG;
private static final Yaml _YAML_OPEN_API;
static {
Representer representer = new Representer(new DumperOptions());
PropertyUtils propertyUtils = representer.getPropertyUtils();
propertyUtils.setSkipMissingProperties(true);
LoaderOptions loaderOptions = new LoaderOptions();
loaderOptions.setAllowDuplicateKeys(false);
Constructor configYAMLConstructor = new Constructor(
ConfigYAML.class, loaderOptions);
TypeDescription securityTypeDescription = new TypeDescription(
Security.class);
securityTypeDescription.substituteProperty(
"oAuth2", String.class, "getOAuth2", "setOAuth2");
configYAMLConstructor.addTypeDescription(securityTypeDescription);
_YAML_CONFIG = new Yaml(
configYAMLConstructor, representer, new DumperOptions(),
loaderOptions);
Constructor openAPIYAMLConstructor = new Constructor(
OpenAPIYAML.class, loaderOptions);
TypeDescription itemsTypeDescription = new TypeDescription(Items.class);
itemsTypeDescription.substituteProperty(
"$ref", String.class, "getReference", "setReference");
itemsTypeDescription.substituteProperty(
"additionalProperties", Schema.class, "getAdditionalPropertySchema",
"setAdditionalPropertySchema");
itemsTypeDescription.substituteProperty(
"properties", Map.class, "getPropertySchemas",
"setPropertySchemas");
itemsTypeDescription.addPropertyParameters(
"properties", String.class, Schema.class);
openAPIYAMLConstructor.addTypeDescription(itemsTypeDescription);
TypeDescription openAPIYAMLTypeDescription = new TypeDescription(
OpenAPIYAML.class);
openAPIYAMLTypeDescription.substituteProperty(
"paths", Map.class, "getPathItems", "setPathItems");
openAPIYAMLTypeDescription.addPropertyParameters(
"paths", String.class, PathItem.class);
openAPIYAMLConstructor.addTypeDescription(openAPIYAMLTypeDescription);
TypeDescription parameterTypeDescription = new TypeDescription(
Parameter.class);
parameterTypeDescription.substituteProperty(
"$ref", String.class, "getReference", "setReference");
openAPIYAMLConstructor.addTypeDescription(parameterTypeDescription);
// Schema
TypeDescription schemaTypeDescription = new TypeDescription(
Schema.class);
schemaTypeDescription.substituteProperty(
"$ref", String.class, "getReference", "setReference");
schemaTypeDescription.substituteProperty(
"additionalProperties", Schema.class, "getAdditionalPropertySchema",
"setAdditionalPropertySchema");
schemaTypeDescription.substituteProperty(
"allOf", List.class, "getAllOfSchemas", "setAllOfSchemas");
schemaTypeDescription.addPropertyParameters("allOf", Schema.class);
schemaTypeDescription.substituteProperty(
"anyOf", List.class, "getAnyOfSchemas", "setAnyOfSchemas");
schemaTypeDescription.addPropertyParameters("anyOf", Schema.class);
schemaTypeDescription.substituteProperty(
"enum", List.class, "getEnumValues", "setEnumValues");
schemaTypeDescription.addPropertyParameters("enum", String.class);
schemaTypeDescription.substituteProperty(
"oneOf", List.class, "getOneOfSchemas", "setOneOfSchemas");
schemaTypeDescription.addPropertyParameters("oneOf", Schema.class);
schemaTypeDescription.substituteProperty(
"properties", Map.class, "getPropertySchemas",
"setPropertySchemas");
schemaTypeDescription.addPropertyParameters(
"properties", String.class, Schema.class);
schemaTypeDescription.substituteProperty(
"required", List.class, "getRequiredPropertySchemaNames",
"setRequiredPropertySchemaNames");
schemaTypeDescription.addPropertyParameters("required", String.class);
schemaTypeDescription.substituteProperty(
"x-json-map", boolean.class, "isJsonMap", "setJsonMap");
schemaTypeDescription.substituteProperty(
"x-json-string", boolean.class, "isJsonString", "setJsonString");
schemaTypeDescription.substituteProperty(
"x-merge-properties", boolean.class, "isMergeProperties",
"setMergeProperties");
schemaTypeDescription.substituteProperty(
"xml", XML.class, "getXML", "setXML");
schemaTypeDescription.addPropertyParameters(
"xml", String.class, XML.class);
openAPIYAMLConstructor.addTypeDescription(schemaTypeDescription);
_YAML_OPEN_API = new Yaml(
openAPIYAMLConstructor, representer, new DumperOptions(),
loaderOptions);
}
}