1

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!

  • Please post the actual OpenAPI file generated from your Java code. You can export it as [explained here](https://stackoverflow.com/a/48525934/113116). It looks like the `Player.id` property in your actual OpenAPI file doesn't have `readOnly` attribute. – Helen Oct 04 '21 at 07:28
  • @Helen thank you! Yes, in json there is no readOnly, but what i did wrong? I have posted in question json for Player – Developer1234567 Oct 04 '21 at 08:23
  • What version of the swagger-core and/or Springfox libraries do you use? – Helen Oct 04 '21 at 08:29
  • @Helen io.springfox:springfox-swagger2:2.9.2, io.springfox:springfox-swagger-ui:2.9.2, io.swagger.core.v3:swagger-annotations:2.1.6 – Developer1234567 Oct 04 '21 at 08:36
  • @Helen Do you think this is because of the version? – Developer1234567 Oct 04 '21 at 09:40
  • 1
    @Helen Updated to the latest version and it is not working ((( if I use deprecated readonly property of Schema annotation in UI everything is ok, but I can send POST request with readonly filed and no 400 error is not thrown – Developer1234567 Oct 05 '21 at 06:30

0 Answers0