Specification yml file:
openapi: 3.0.3
info:
version: 0.0.0
title: Game
paths:
/players:
post:
summary: Create a player
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/Player'
responses:
'201':
description: Created
content:
application/json:
schema:
$ref: '#/components/schemas/Player'
components:
schemas:
Player:
type: object
properties:
id:
type: integer
example: 1
readOnly: true
playername:
type: string
example: Jack
Model class:
@Validated
public class Player {
@JsonProperty(value = "id")
@Schema(example = "1", accessMode = Schema.AccessMode.READ_ONLY)
private Integer id;
@Schema(example = "Jack")
@JsonProperty(value = "playername")
@NotNull
private String playername;
I have POST endpoint in Controller:
@Operation(summary = "Create a player", description = "", tags={ })
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "Created", content = @Content(mediaType = "application/json", schema = @Schema(implementation = Player.class))) })
@RequestMapping(value = "/players",
produces = { "application/json" },
consumes = { "application/json" },
method = RequestMethod.POST)
public ResponseEntity<Player> playersPost(
@Parameter(in = ParameterIn.DEFAULT, description = "", required = true, schema = @Schema())
@Valid @RequestBody Player body) {
String accept = request.getHeader("Accept");
if (accept != null && accept.contains("application/json")) {
return ResponseEntity.ok(body);
}
return new ResponseEntity<Player>(HttpStatus.NOT_IMPLEMENTED);
}
According to documentation Swagger Doc : readOnly properties are included in responses but not in requests
But this filed in Swagger UI is included in the request body and I can see it in example sections in Swagger UI of POST request.
I can send POST request with readonly filed and no exception is thrown. Swagger POST img
OpenAPI file /api-docs JSON for Player:
definitions: {
Player: {
type: "object",
properties: {
id: {
type: "integer",
format: "int32",
},
playername: {
type: "string"
},
},
title: "Player",
}
}
Dependencies:
implementation "io.springfox:springfox-swagger2:2.9.2"
implementation "io.springfox:springfox-swagger-ui:2.9.2"
implementation "io.swagger.core.v3:swagger-annotations:2.1.6"
What have I done wrong? Thank you!