Skip to content

Commit

Permalink
changes report #1816
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Aug 25, 2022
1 parent c0947aa commit 7de3935
Show file tree
Hide file tree
Showing 5 changed files with 238 additions and 32 deletions.
@@ -0,0 +1,145 @@
/*
*
* *
* * *
* * * * Copyright 2019-2022 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
* * * *
* * * * https://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.springdoc.core.models;

import java.util.Objects;

import io.swagger.v3.oas.models.parameters.Parameter;
import org.apache.commons.lang3.StringUtils;

/**
* The type Parameter Id.
* @author bnasslahsen
*/
public class ParameterId {

/**
* The P name.
*/
private String pName;

/**
* The Param type.
*/
private String paramType;

/**
* The Ref.
*/
private String $ref;

/**
* Instantiates a new Parameter id.
*
* @param parameter the parameter
*/
public ParameterId(Parameter parameter) {
this.pName = parameter.getName();
this.paramType = parameter.getIn();
this.$ref = parameter.get$ref();
}

/**
* Instantiates a new Parameter id.
*
* @param parameter the parameter
*/
public ParameterId(io.swagger.v3.oas.annotations.Parameter parameter) {
this.pName = parameter.name();
this.paramType = parameter.in().toString();
this.$ref = parameter.ref();
}

/**
* Gets name.
*
* @return the name
*/
public String getpName() {
return pName;
}

/**
* Sets name.
*
* @param pName the p name
*/
public void setpName(String pName) {
this.pName = pName;
}

/**
* Gets param type.
*
* @return the param type
*/
public String getParamType() {
return paramType;
}

/**
* Sets param type.
*
* @param paramType the param type
*/
public void setParamType(String paramType) {
this.paramType = paramType;
}

/**
* Get ref string.
*
* @return the string
*/
public String get$ref() {
return $ref;
}

/**
* Set ref.
*
* @param $ref the ref
*/
public void set$ref(String $ref) {
this.$ref = $ref;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ParameterId that = (ParameterId) o;
if (this.pName == null && StringUtils.isBlank(this.paramType))
return Objects.equals($ref, that.$ref);
if (this.pName != null && StringUtils.isBlank(this.paramType))
return Objects.equals(pName, that.pName);

return Objects.equals(pName, that.pName) && Objects.equals(paramType, that.paramType);

}

@Override
public int hashCode() {
return Objects.hash(pName, paramType, $ref);
}
}
Expand Up @@ -68,6 +68,7 @@
import org.springdoc.core.customizers.ParameterCustomizer;
import org.springdoc.core.extractor.DelegatingMethodParameter;
import org.springdoc.core.models.MethodAttributes;
import org.springdoc.core.models.ParameterId;
import org.springdoc.core.models.ParameterInfo;
import org.springdoc.core.models.RequestBodyInfo;
import org.springdoc.core.providers.JavadocProvider;
Expand Down Expand Up @@ -328,24 +329,24 @@ else if (!RequestMethod.GET.equals(requestMethod)) {
}
}

LinkedHashMap<String, Parameter> map = getParameterLinkedHashMap(components, methodAttributes, operationParameters, parametersDocMap);
LinkedHashMap<ParameterId, Parameter> map = getParameterLinkedHashMap(components, methodAttributes, operationParameters, parametersDocMap);
RequestBody requestBody = requestBodyInfo.getRequestBody();
// support form-data
if (defaultSupportFormData && requestBody != null
&& requestBody.getContent() != null
&& requestBody.getContent().containsKey(org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE)) {
Iterator<Entry<String, Parameter>> it = map.entrySet().iterator();
Iterator<Entry<ParameterId, Parameter>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, Parameter> entry = it.next();
Entry<ParameterId, Parameter> entry = it.next();
Parameter parameter = entry.getValue();
if (!ParameterIn.PATH.toString().equals(parameter.getIn())) {
io.swagger.v3.oas.models.media.Schema<?> itemSchema = new io.swagger.v3.oas.models.media.Schema() ;
itemSchema.setName(entry.getKey());
io.swagger.v3.oas.models.media.Schema<?> itemSchema = new io.swagger.v3.oas.models.media.Schema<>();
itemSchema.setName(entry.getKey().getpName());
itemSchema.setDescription(parameter.getDescription());
itemSchema.setDeprecated(parameter.getDeprecated());
if (parameter.getExample() != null)
itemSchema.setExample(parameter.getExample());
requestBodyInfo.addProperties(entry.getKey(), itemSchema);
requestBodyInfo.addProperties(entry.getKey().getpName(), itemSchema);
it.remove();
}
}
Expand All @@ -363,27 +364,32 @@ else if (!RequestMethod.GET.equals(requestMethod)) {
* @param parametersDocMap the parameters doc map
* @return the parameter linked hash map
*/
private LinkedHashMap<String, Parameter> getParameterLinkedHashMap(Components components, MethodAttributes methodAttributes, List<Parameter> operationParameters, Map<String, io.swagger.v3.oas.annotations.Parameter> parametersDocMap) {
LinkedHashMap<String, Parameter> map = operationParameters.stream()
private LinkedHashMap<ParameterId, Parameter> getParameterLinkedHashMap(Components components, MethodAttributes methodAttributes, List<Parameter> operationParameters, Map<String, io.swagger.v3.oas.annotations.Parameter> parametersDocMap) {
LinkedHashMap<ParameterId, Parameter> map = operationParameters.stream()
.collect(Collectors.toMap(
parameter -> parameter.getName() != null ? parameter.getName() : Integer.toString(parameter.hashCode()),
parameter -> parameter,
ParameterId::new,
parameter -> parameter,
(u, v) -> {
throw new IllegalStateException(String.format("Duplicate key %s", u));
},
LinkedHashMap::new
));

for (Map.Entry<String, io.swagger.v3.oas.annotations.Parameter> entry : parametersDocMap.entrySet()) {
if (entry.getKey() != null && !map.containsKey(entry.getKey()) && !entry.getValue().hidden()) {
ParameterId parameterId = new ParameterId(entry.getValue());
if (entry.getKey() != null && !map.containsKey(parameterId) && !entry.getValue().hidden()) {
//Convert
Parameter parameter = parameterBuilder.buildParameterFromDoc(entry.getValue(), components,
methodAttributes.getJsonViewAnnotation(), methodAttributes.getLocale());
map.put(entry.getKey(), parameter);
map.put(parameterId, parameter);
}
}

getHeaders(methodAttributes, map);
map.forEach((parameterId, parameter) -> {
if(StringUtils.isBlank(parameter.getIn()) && StringUtils.isBlank(parameter.get$ref()))
parameter.setIn(ParameterIn.QUERY.toString());
});
return map;
}

Expand All @@ -395,22 +401,23 @@ private LinkedHashMap<String, Parameter> getParameterLinkedHashMap(Components co
* @return the headers
*/
@SuppressWarnings("unchecked")
public static Collection<Parameter> getHeaders(MethodAttributes methodAttributes, Map<String, Parameter> map) {
public static Collection<Parameter> getHeaders(MethodAttributes methodAttributes, Map<ParameterId, Parameter> map) {
for (Map.Entry<String, String> entry : methodAttributes.getHeaders().entrySet()) {
StringSchema schema = new StringSchema();
if (StringUtils.isNotEmpty(entry.getValue()))
schema.addEnumItem(entry.getValue());
Parameter parameter = new Parameter().in(ParameterIn.HEADER.toString()).name(entry.getKey()).schema(schema);
if (map.containsKey(entry.getKey())) {
parameter = map.get(entry.getKey());
ParameterId parameterId = new ParameterId(parameter);
if (map.containsKey(parameterId)) {
parameter = map.get(parameterId);
List existingEnum = null;
if (parameter.getSchema() != null && !CollectionUtils.isEmpty(parameter.getSchema().getEnum()))
existingEnum = parameter.getSchema().getEnum();
if (StringUtils.isNotEmpty(entry.getValue()) && (existingEnum==null || !existingEnum.contains(entry.getValue())))
if (StringUtils.isNotEmpty(entry.getValue()) && (existingEnum == null || !existingEnum.contains(entry.getValue())))
parameter.getSchema().addEnumItemObject(entry.getValue());
parameter.setSchema(parameter.getSchema());
}
map.put(entry.getKey(), parameter);
map.put(parameterId, parameter);
}
return map.values();
}
Expand Down Expand Up @@ -508,7 +515,7 @@ public Parameter buildParams(ParameterInfo parameterInfo, Components components,
// By default
if (!isRequestBodyParam(requestMethod, parameterInfo)) {
parameterInfo.setRequired(!((DelegatingMethodParameter) methodParameter).isNotRequired() && !methodParameter.isOptional());
parameterInfo.setParamType(QUERY_PARAM);
//parameterInfo.setParamType(QUERY_PARAM);
parameterInfo.setDefaultValue(null);
return this.buildParam(parameterInfo, components, jsonView);
}
Expand Down
Expand Up @@ -178,7 +178,12 @@ public static Parameter mergeParameter(List<Parameter> existingParamDoc, Paramet
Parameter result = paramCalcul;
if (paramCalcul != null && paramCalcul.getName() != null) {
final String name = paramCalcul.getName();
Parameter paramDoc = existingParamDoc.stream().filter(p -> name.equals(p.getName())).findAny().orElse(null);
final String in = paramCalcul.getIn();
Parameter paramDoc = existingParamDoc.stream().filter(p ->
name.equals(p.getName())
&& (StringUtils.isEmpty(in) || StringUtils.isEmpty(p.getIn()) || in.equals(p.getIn()))
).findAny()
.orElse(null);
if (paramDoc != null) {
mergeParameter(paramCalcul, paramDoc);
result = paramDoc;
Expand Down
Expand Up @@ -2,21 +2,19 @@
*
* *
* * *
* * * * Copyright 2019-2022 the original author or authors.
* * * *
* * * * * Copyright 2019-2022 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
* * * * *
* * * * * https://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.
* * * * 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
* * * *
* * * * https://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.
* * *
* *
*
Expand All @@ -27,12 +25,17 @@
import java.util.UUID;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;


Expand All @@ -51,6 +54,19 @@ public ResponseEntity<Item> putItem(
return ResponseEntity.ok(item);
}

/**
* List tracker data.
*
* @return the tracker data
*/
@GetMapping(value = "/values/data")
void list(@RequestHeader(value = "access_token", required = false)
@Parameter(name = "access_token", in = ParameterIn.HEADER, description = "token in header", schema = @Schema(implementation = String.class))
String tokenInHeader,@CookieValue(value = "access_token", required = false)
@Parameter(name = "access_token", in = ParameterIn.COOKIE, description = "token in cookie", schema = @Schema(implementation = String.class))
String tokenInCookie) {

}
public static class Item {
}
}
Expand Up @@ -58,6 +58,39 @@
}
}
}
},
"/values/data": {
"get": {
"tags": [
"hello-controller"
],
"operationId": "list",
"parameters": [
{
"name": "access_token",
"in": "header",
"description": "token in header",
"required": false,
"schema": {
"type": "string"
}
},
{
"name": "access_token",
"in": "cookie",
"description": "token in cookie",
"required": false,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK"
}
}
}
}
},
"components": {
Expand Down

0 comments on commit 7de3935

Please sign in to comment.