Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a FormParameter model object instead of BodyParameter #2722

Merged
merged 4 commits into from
Jan 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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;
Expand All @@ -35,19 +36,72 @@
import java.util.List;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

import static springfox.documentation.schema.Types.*;
import static springfox.documentation.swagger2.mappers.EnumMapper.*;
import static springfox.documentation.swagger2.mappers.Properties.*;
import static java.util.stream.Collectors.toSet;
import static springfox.documentation.schema.Types.isBaseType;
import static springfox.documentation.swagger2.mappers.EnumMapper.maybeAddAllowableValues;
import static springfox.documentation.swagger2.mappers.EnumMapper.maybeAddAllowableValuesToParameter;
import static springfox.documentation.swagger2.mappers.Properties.itemTypeProperty;
import static springfox.documentation.swagger2.mappers.Properties.property;

@Mapper
public class ParameterMapper {

// This list is directly copied from the OpenAPI 2.0 spec
private static final Set<String> supportedFormDataTypes = Stream.of(
"string",
"number",
"integer",
"boolean",
"array",
"file").collect(toSet());

private static final VendorExtensionsMapper vendorMapper = new VendorExtensionsMapper();

public Parameter mapParameter(springfox.documentation.service.Parameter source) {
Parameter bodyParameter = bodyParameter(source);
return SerializableParameterFactories.create(source).orElse(bodyParameter);
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
ModelReference modelRef = source.getModelRef();
parameter.setProperty(itemTypeProperty(modelRef));

if (!supportedFormDataTypes.contains(parameter.getType())
|| "array".equals(parameter.getType()) && !supportedFormDataTypes.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(vendorMapper.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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package springfox.documentation.swagger2.mappers
import com.fasterxml.classmate.ResolvedType
import io.swagger.models.Model
import io.swagger.models.parameters.BodyParameter
import io.swagger.models.parameters.FormParameter
import io.swagger.models.parameters.QueryParameter
import io.swagger.models.parameters.SerializableParameter
import spock.lang.Specification
Expand Down Expand Up @@ -37,6 +38,54 @@ class ParameterMapperSpec extends Specification {
"body" | new ModelRef("sometype", new ModelRef("itemType"), true) | BodyParameter
}

def "form parameters are mapped correctly" () {
given:
def parameter = parameter("formData").modelRef(modelRef).build()
when:
def sut = new ParameterMapper()
then:
def mapped = (FormParameter) sut.mapParameter(parameter)
and:
mapped.access == "access"
mapped.name == "test"
mapped.description == "test description"
mapped.required
mapped.type == type
mapped.format == format
mapped.items?.format == itemFormat
mapped.items?.type == itemType

where:
modelRef | type | format | itemType | itemFormat
new ModelRef("string") | "string" | null | null | null
new ModelRef("array", new ModelRef("string")) | "array" | null | "string" | null
new ModelRef("array", new ModelRef("int")) | "array" | null | "integer" | "int32"
new ModelRef("int") | "integer" | "int32" | null | null
new ModelRef("long") | "integer" | "int64" | null | null
}

def "form parameters fall back to body parameters for non-primitive top level types" () {
given:
def parameter = parameter("formData")
.modelRef(new ModelRef("some-non-primitive-type"))
.build()
when:
def sut = new ParameterMapper()
then:
sut.mapParameter(parameter) instanceof BodyParameter
}

def "form parameters fall back to body parameters for arrays of non-primitive types" () {
given:
def parameter = parameter("formData")
.modelRef(new ModelRef("array", new ModelRef("object")))
.build()
when:
def sut = new ParameterMapper()
then:
sut.mapParameter(parameter) instanceof BodyParameter
}

def "Serializes byte array to string model in body" () {
given:
def byteArray = new ModelRef("", new ModelRef("byte"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1119,10 +1119,8 @@
"name": "sfId",
"description": "sfId",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
"type": "integer",
"format": "int32"
}
],
"responses": {
Expand Down Expand Up @@ -1152,91 +1150,72 @@
"name": "allCapsSet",
"description": "description of allCapsSet",
"required": false,
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi"
},
{
"in": "formData",
"name": "annotatedEnumType",
"description": "description of annotatedEnumType",
"required": false,
"schema": {
"type": "string"
}
"type": "string"
},
{
"in": "formData",
"name": "bar",
"description": "description of bar",
"required": false,
"schema": {
"type": "integer",
"format": "int32"
}
"type": "integer",
"format": "int32"
},
{
"in": "formData",
"name": "enumType",
"required": false,
"schema": {
"type": "string"
}
"type": "string"
},
{
"in": "formData",
"name": "foo",
"description": "description of foo",
"required": true,
"schema": {
"type": "string"
}
"type": "string"
},
{
"in": "formData",
"name": "localDateTime",
"description": "local date time desc dd-MM-yyyy hh:mm:ss",
"required": true,
"schema": {
"type": "string",
"format": "date-time"
}
"type": "string",
"format": "date-time"
},
{
"in": "formData",
"name": "nestedType.name",
"required": false,
"schema": {
"type": "string"
}
"type": "string"
},
{
"in": "formData",
"name": "parentBeanProperty",
"required": false,
"schema": {
"type": "string"
}
"type": "string"
},
{
"in": "formData",
"name": "propertyWithNoSetterMethod",
"required": false,
"schema": {
"type": "string"
}
"type": "string"
},
{
"in": "formData",
"name": "readOnlyString",
"description": "A read only string",
"required": false,
"schema": {
"type": "string"
}
"type": "string"
}
],
"responses": {
Expand Down Expand Up @@ -1465,8 +1444,8 @@
"/bugs/2268{?$filter}": {
"get": {
"tags": [
"example",
"Bugs"
"Bugs",
"example"
],
"summary": "Get all examples",
"description": "Get all examples ",
Expand Down