Skip to content

OASModelReader Samples

Arthur De Magalhaes edited this page May 29, 2018 · 2 revisions

The TCK tests for microprofile-open-api contains a comprehensive example of Model Reader implementation based on the sample Airlines app.

Below is another sample implementation of an OASModelReader. This programmatic model can specify the entire OpenAPI object or just some parts of it, with other parts augmented by in-app annotations and static documentation.

public class SampleOASModelReaderImplOne implements OASModelReader {

    @Override
    public OpenAPI buildModel() {
        return OASFactory.createObject(OpenAPI.class)
                .info(OASFactory.createObject(Info.class)
                    .title("Sample App defined with Model Reader Implementation")
                    .version("1.0")
                    .termsOfService("http://termsofservice.com/terms")
                    .contact(OASFactory.createObject(Contact.class)
                        .name("Sample API Support")
                        .url("http://sampleappone.com/contact")
                        .email("techsupport@sampleappone.com"))
                    .license(OASFactory.createObject(License.class)
                        .name("Apache 2.0")
                        .url("http://www.apache.org/licenses/LICENSE-2.0.html")))
                .security(new ArrayList<SecurityRequirement>())
                    .addSecurityRequirement(OASFactory.createObject(SecurityRequirement.class)
                        .addScheme("airlinesRatingApp_auth"))
                .servers(new ArrayList<Server>())
                    .addServer(OASFactory.createObject(Server.class)
                        .url("https://{username}.sampleserver.com:{port}/{basePath}")
                        .description("API server for sample app")
                        .variables(OASFactory.createObject(ServerVariables.class)
                            .addServerVariable("username", OASFactory.createObject(ServerVariable.class)
                                .defaultValue("user")
                                .description("Reviews of the app by users")
                                .enumeration(new ArrayList<String>())
                                    .addEnumeration("user1")
                                    .addEnumeration("user2"))
                .components(OASFactory.createObject(Components.class)
                    .schemas(new HashMap<String, Schema>())
                        .addSchema("Users", OASFactory.createObject(Schema.class)
                            .type(Schema.SchemaType.STRING)
                            .title("App Users")
                            .ref("#/components.schemas.Users"))
                    .responses(new HashMap<String, APIResponse>())
                        .addResponse("FoundUser", OASFactory.createObject(APIResponse.class)
                            .description("Found listed user")
                            .content(OASFactory.createObject(Content.class)
                                .addMediaType("application/json", OASFactory.createObject(MediaType.class)
                                    .schema(OASFactory.createObject(Schema.class)
                                        .type(Schema.SchemaType.Object)))))
                    .parameters(new HashMap<String, Parameter>())
                        .addParameter("AppUser", OASFactory.createObject(Parameter.class)
                            .required(true)
                            .description("User profile")
                            .schema(OASFactory.createObject(Schema.class)))
                    .examples(new HashMap<String, Example>())
                        .addExample("Bob Smith", OASFactory.createObject(Example.class)
                            .summary("App profile of Bob Smith")
                            .description("This example demonstrates the format of a user profile")
                            .externalValue("http://sampleapp.com/examples/user-example.json"))
                    .requestBodies(new HashMap<String, RequestBody>())
                        .addRequestBody("user", OASFactory.createObject(RequestBody.class)
                            .required(true)
                            .description("example user profile to add")
                            .content(OASFactory.createObject(Content.class)
                                .addMediaType("application/json", OASFactory.createObject(MediaType.class)
                                    .schema(OASFactory.createObject(Schema.class)
                                        .ref("#/components.schemas.user")))))
                    .headers(new HashMap<String, Header>())
                        .addHeader("Max-Rate", OASFactory.createObject(Header.class)
                            .description("Maximum rate")
                            .schema(OASFactory.createObject(Schema.class)
                                .type(Schema.SchemaType.INTEGER))
                            .required(true)
                            .allowEmptyValue(true)
                            .deprecated(true))
                    .securitySchemes(new HashMap<String, SecurityScheme>())
                        .addSecurityScheme("httpTestScheme", OASFactory.createObject(SecurityScheme.class)
                            .description("user security scheme")
                            .type(SecurityScheme.Type.HTTP)
                            .scheme("testScheme"))
                    .links(new HashMap<String, Link>())
                        .addLink("UserName", OASFactory.createObject(Link.class)
                            .description("The username corresponding to provided user id")
                            .operationId("getUserByName")
                            .parameters(new HashMap<String, Object>())
                                .addParameter("userId", "$request.link-path.userId")))
                    .tags(new ArrayList<Tag>())
                        .addTag(OASFactory.createObject(Tag.class)
                            .name("Get Users")
                            .description("method to get all user profiles registered in the app"))
                    .externalDocs(OASFactory.createObject(ExternalDocumentation.class)
                        .description("Instructions for how to deploy this app")
                        .url("https://sampleapp.com/deployingapp"))
                    .paths(OASFactory.createObject(Paths.class)
                        .addPathItem("/appUsers", OASFactory.createObject(PathItem.class)
                            .GET(OASFactory.createObject(Operation.class)
                                .summary("Retrieve all user profiles")
                                .operationId("getUsers")
                                .responses(OASFactory.createObject(APIResponses.class)
                                    .addApiResponse("404", OASFactory.createObject(APIResponse.class)
                                        .description("No users found")
                                        .content(OASFactory.createObject(Content.class)
                                            .addMediaType("n/a", OASFactory.createObject(MediaType.class)))))))
}}
Clone this wiki locally