Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open API 3.0.1 readOnly Annotation

I am having a few problems with the Open API 3.0.1 java annotations to define a property as readOnly for a specific method. During a POST operation the API expects a NewAccount model. Here is a truncated representation of the model;

{
    "user": {
        "id": "...",
        ...
    },
    "account": {
        "id": "...",
        ...
    }
}

What I need is to define the user.id and account.id as readOnly in this operation. All my docs are generated from code via annotations and this is what I have so far;

    @POST
    @Path("/access/accounts")
    @Consumes("application/json")
    @Operation(
            summary = "Create a new account.",
            description = "Create a new account."
    )
    @Tags({@Tag(name = "account")})
    @RequestBody(
            description = "New accounts required models.",
            required = true,
            content = @Content(
                    schema = @Schema(
                            implementation = NewAccount.class
                    )
            )
    )
    @ApiResponses({
            @ApiResponse(
                    responseCode = "201",
                    description = "The new account.",
                    content = @Content(
                            schema = @Schema(
                                    implementation = Account.class
                            )
                    )
            )
    })
like image 855
tarka Avatar asked May 16 '26 00:05

tarka


1 Answers

You need to add the @Schema(accessMode = Schema.AccessMode.READ_ONLY) annotation to the id property. This will mark the id property as readOnly: true in the generated OpenAPI definition.

Example from here:

import io.swagger.v3.oas.annotations.media.Schema;

public class ReadOnlyFields {
    @Schema(accessMode = Schema.AccessMode.READ_ONLY)
    public Long id;
}
like image 176
Helen Avatar answered May 18 '26 15:05

Helen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!