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

Fix #2822 related to @ApiParam(example="..") not working for @Request… #2824

Merged
merged 1 commit into from
Dec 17, 2018
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 @@ -496,6 +496,11 @@ public ResponseEntity<String> bug2415(
public void bug2423(Bug2423 input) {
}

@PostMapping("/2822")
public void bug2822(@ApiParam(example = "exampleMessage") @RequestBody String message) {
// Empty body is sufficient for testing
}

public class Bug2423 {
public String from;
public String to;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
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 springfox.documentation.schema.ModelReference;

Expand All @@ -54,7 +56,7 @@ private Parameter bodyParameter(springfox.documentation.service.Parameter source
BodyParameter parameter = new BodyParameter()
.description(source.getDescription())
.name(source.getName())
.schema(fromModelRef(source.getModelRef()));
.schema(toSchema(source));
parameter.setIn(source.getParamType());
parameter.setAccess(source.getParamAccess());
parameter.setPattern(source.getPattern());
Expand All @@ -71,6 +73,20 @@ private Parameter bodyParameter(springfox.documentation.service.Parameter source
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(ModelReference modelRef) {
if (modelRef.isCollection()) {
if (modelRef.getItemType().equals("byte")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ class ParameterMapperSpec extends Specification {
mapped.schema.type == "string"
}

def "Maps example for body parameter" () {
given:
def parameter = parameter("body")
.modelRef(new ModelRef("sometype"))
.scalarExample("example")
.build()
when:
def sut = new ParameterMapper()
then:
def mapped = sut.mapParameter(parameter)
and:
mapped.access == "access"
mapped.name == "test"
mapped.description == "test description"
mapped.required
mapped instanceof BodyParameter
mapped.schema instanceof Model
mapped.schema.example == "example"
}

def "Serializes byte array to string model in query" () {
given:
def byteArray = new ModelRef("", new ModelRef("byte"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,8 @@
"required": false,
"schema": {
"type": "integer",
"format": "int32"
"format": "int32",
"example": "10"
}
},
{
Expand Down Expand Up @@ -1694,6 +1695,41 @@
"deprecated": false
}
},
"/bugs/2822":{
"post":{
"tags":[
"Bugs"
],
"summary":"bug2822",
"operationId":"bug2822UsingPOST_2",
"consumes":[
"application/json"
],
"parameters":[
{
"in":"body",
"name":"message",
"description":"message",
"required":false,
"schema":{
"type":"string",
"example":{
"value":"exampleMessage",
"mediaType":{
"present":false
}
}
}
}
],
"responses":{
"200":{
"description":"OK"
}
},
"deprecated":false
}
},
"/bugs/bug1827{?authorIds,authors[0].books[0].id,authors[0].books[0].name,authors[0].id,authors[0].name,id,name}": {
"get": {
"tags": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,8 @@
"required": false,
"schema": {
"type": "integer",
"format": "int32"
"format": "int32",
"example": "10"
}
},
{
Expand Down Expand Up @@ -1703,6 +1704,41 @@
"deprecated": false
}
},
"/bugs/2822":{
"post":{
"tags":[
"Bugs"
],
"summary":"bug2822",
"operationId":"bug2822UsingPOST_1",
"consumes":[
"application/json"
],
"parameters":[
{
"in":"body",
"name":"message",
"description":"message",
"required":false,
"schema":{
"type":"string",
"example":{
"value":"exampleMessage",
"mediaType":{
"present":false
}
}
}
}
],
"responses":{
"200":{
"description":"OK"
}
},
"deprecated":false
}
},
"/bugs/bug1827{?authorIds,authors[0].books[0].id,authors[0].books[0].name,authors[0].id,authors[0].name,id,name}": {
"get": {
"tags": [
Expand Down