-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ParameterMapper.java
181 lines (159 loc) · 7.03 KB
/
ParameterMapper.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
/*
*
* Copyright 2015-2019 the original author or authors.
*
* 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 springfox.documentation.swagger2.mappers;
import io.swagger.models.ArrayModel;
import io.swagger.models.Model;
import io.swagger.models.ModelImpl;
import io.swagger.models.RefModel;
import io.swagger.models.parameters.BodyParameter;
import io.swagger.models.parameters.FormParameter;
import io.swagger.models.parameters.Parameter;
import io.swagger.models.properties.FileProperty;
import io.swagger.models.properties.Property;
import org.mapstruct.Mapper;
import org.springframework.util.StringUtils;
import springfox.documentation.schema.Example;
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import static java.util.stream.Collectors.*;
import static springfox.documentation.swagger2.mappers.EnumMapper.*;
/**
* Use {@link RequestParameterMapper} instead
* @deprecated @since 3.0.0
*/
@Deprecated
@Mapper(componentModel = "spring")
public class ParameterMapper {
// This list is directly copied from the OpenAPI 2.0 spec
private static final Set<String> SUPPORTED_FORM_DATA_TYPES = Stream.of(
"string",
"number",
"integer",
"boolean",
"array",
"file").collect(toSet());
private static final VendorExtensionsMapper VENDOR_EXTENSIONS_MAPPER = new VendorExtensionsMapper();
public Parameter mapParameter(springfox.documentation.service.Parameter source) {
Parameter parameter;
if ("formData".equals(source.getParamType())) {
parameter = formParameter(source);
} else {
parameter = bodyParameter(source);
}
return SerializableParameterFactories.create(source).orElse(parameter);
}
private Parameter formParameter(springfox.documentation.service.Parameter source) {
FormParameter parameter = new FormParameter()
.name(source.getName())
.description(source.getDescription());
// Form Parameters only work with certain primitive types specified in the spec
springfox.documentation.schema.ModelReference modelRef = source.getModelRef();
parameter.setProperty(springfox.documentation.swagger2.mappers.Properties.itemTypeProperty(modelRef));
if (!SUPPORTED_FORM_DATA_TYPES.contains(parameter.getType())
|| "array".equals(parameter.getType())
&& !SUPPORTED_FORM_DATA_TYPES.contains(parameter.getItems().getType())) {
// Falling back to BodyParameter is non-compliant with the Swagger 2.0 spec,
// but matches previous behavior.
return bodyParameter(source);
}
parameter.setIn(source.getParamType());
parameter.setAccess(source.getParamAccess());
parameter.setPattern(source.getPattern());
parameter.setRequired(source.isRequired());
parameter.getVendorExtensions().putAll(VENDOR_EXTENSIONS_MAPPER.mapExtensions(source.getVendorExtentions()));
for (Entry<String, List<Example>> each : source.getExamples().entrySet()) {
Optional<Example> example = each.getValue().stream().findFirst();
if (example.isPresent() && example.get().getValue() != null) {
// Form parameters only support a single example
parameter.example(String.valueOf(example.get().getValue()));
break;
}
}
return parameter;
}
private Parameter bodyParameter(springfox.documentation.service.Parameter source) {
BodyParameter parameter = new BodyParameter()
.description(source.getDescription())
.name(source.getName())
.schema(toSchema(source));
parameter.setIn(source.getParamType());
parameter.setAccess(source.getParamAccess());
parameter.setPattern(source.getPattern());
parameter.setRequired(source.isRequired());
parameter.getVendorExtensions().putAll(VENDOR_EXTENSIONS_MAPPER.mapExtensions(source.getVendorExtentions()));
for (Entry<String, List<Example>> each : source.getExamples().entrySet()) {
Optional<Example> example = each.getValue().stream().findFirst();
if (example.isPresent() && example.get().getValue() != null) {
parameter.addExample(each.getKey(), String.valueOf(example.get().getValue()));
}
}
return parameter;
}
private Model toSchema(springfox.documentation.service.Parameter source) {
Model schema = fromModelRef(source.getModelRef());
if (!StringUtils.isEmpty(source.getScalarExample()) && !isEmptyExample(source.getScalarExample())) {
schema.setExample(source.getScalarExample());
}
return schema;
}
private boolean isEmptyExample(Object object) {
return object instanceof Example && StringUtils.isEmpty(((Example) object).getValue());
}
Model fromModelRef(springfox.documentation.schema.ModelReference modelRef) {
if (modelRef.isCollection()) {
if (modelRef.getItemType().equals("byte")) {
ModelImpl baseModel = new ModelImpl();
baseModel.setType("string");
baseModel.setFormat("byte");
return maybeAddAllowableValuesToParameter(baseModel, modelRef.getAllowableValues());
} else if (modelRef.getItemType().equals("file")) {
ArrayModel files = new ArrayModel();
files.items(new FileProperty());
return files;
}
springfox.documentation.schema.ModelReference itemModel = modelRef.itemModel()
.orElseThrow(() -> new IllegalStateException("ModelRef that is a collection should have an itemModel"));
return new ArrayModel()
.items(maybeAddAllowableValues(
springfox.documentation.swagger2.mappers.Properties.itemTypeProperty(itemModel),
itemModel.getAllowableValues()));
}
if (modelRef.isMap()) {
ModelImpl baseModel = new ModelImpl();
springfox.documentation.schema.ModelReference itemModel = modelRef.itemModel()
.orElseThrow(() -> new IllegalStateException("ModelRef that is a map should have an itemModel"));
baseModel.additionalProperties(
maybeAddAllowableValues(
springfox.documentation.swagger2.mappers.Properties.itemTypeProperty(itemModel),
itemModel.getAllowableValues()));
return baseModel;
}
if (springfox.documentation.schema.Types.isBaseType(modelRef.getType())) {
Property property = springfox.documentation.swagger2.mappers.Properties.property(modelRef.getType());
ModelImpl baseModel = new ModelImpl();
baseModel.setType(property.getType());
baseModel.setFormat(property.getFormat());
return maybeAddAllowableValuesToParameter(baseModel, modelRef.getAllowableValues());
}
return new RefModel(modelRef.getType());
}
}