Skip to content

Commit

Permalink
Fix #2822 related to @ApiParam(example="..") not working for @request…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaladziuk, Stanislau authored and StasKolodyuk committed Dec 15, 2018
1 parent 90c30cc commit d18fda2
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 3 deletions.
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

0 comments on commit d18fda2

Please sign in to comment.