Java Code Examples for io.swagger.v3.oas.models.OpenAPI#components()

The following examples show how to use io.swagger.v3.oas.models.OpenAPI#components() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OpenApiObjectGenerator.java    From flow with Apache License 2.0 6 votes vote down vote up
private OpenAPI createBasicModel() {
    OpenAPI openAPI = new OpenAPI();

    Info info = new Info();
    info.setTitle(configuration.getApplicationTitle());
    info.setVersion(configuration.getApplicationApiVersion());
    openAPI.setInfo(info);

    Paths paths = new Paths();
    openAPI.setPaths(paths);

    Server server = new Server();
    server.setUrl(configuration.getServerUrl());
    server.setDescription(configuration.getServerDescription());
    openAPI.setServers(Collections.singletonList(server));
    Components components = new Components();
    SecurityScheme vaadinConnectOAuth2Scheme = new SecurityScheme()
            .type(SecurityScheme.Type.OAUTH2)
            .flows(new OAuthFlows().password(new OAuthFlow()
                    .tokenUrl(VAADIN_CONNECT_OAUTH2_TOKEN_URL)
                    .scopes(new Scopes())));
    components.addSecuritySchemes(VAADIN_CONNECT_OAUTH2_SECURITY_SCHEME,
            vaadinConnectOAuth2Scheme);
    openAPI.components(components);
    return openAPI;
}
 
Example 2
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testIssue360() {
    OpenAPI openAPI = new OpenAPI();

    Schema model = new Schema();
    model.setEnum((List<String>) null);
    openAPI.components(new Components().addSchemas("modelWithNullEnum", model));

    String json = Json.pretty(openAPI);

    OpenAPIV3Parser parser = new OpenAPIV3Parser();

    SwaggerParseResult result = parser.readContents(json, null, null);
    OpenAPI rebuilt = result.getOpenAPI();
    assertNotNull(rebuilt);
}
 
Example 3
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test(description = "resolve operation parameter remote refs")
public void testOperationParameterRemoteRefs() {
    final OpenAPI swagger = new OpenAPI();
    List<Parameter> parameters = new ArrayList<>();

    parameters.add(new Parameter().$ref("#/components/parameters/SampleParameter"));

    swagger.path("/fun", new PathItem()
            .get(new Operation()
                    .parameters(parameters)));

    swagger.components(new Components().addParameters("SampleParameter", new QueryParameter()
            .name("skip")
            .schema(new IntegerSchema())));

    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();

    final List<Parameter> params = swagger.getPaths().get("/fun").getGet().getParameters();
    assertEquals(params.size(), 1);
    final Parameter param = params.get(0);
    assertEquals(param.getName(), "skip");
}
 
Example 4
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testOperationBodyParameterRemoteRefs() {
    final Schema schema = new Schema();

    final OpenAPI swagger = new OpenAPI();
    swagger.path("/fun", new PathItem()
            .get(new Operation()
                    .parameters(Arrays.asList(new Parameter().$ref("#/components/parameters/SampleParameter")))));

    swagger.path("/times", new PathItem()
            .get(new Operation()
                    .parameters(Arrays.asList(new Parameter().$ref("#/components/parameters/SampleParameter")))));

    swagger.components(new Components().addParameters("SampleParameter", new Parameter()
            .name("skip")
            .schema(schema)));

    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final List<Parameter> params = swagger.getPaths().get("/fun").getGet().getParameters();
    assertEquals(params.size(), 1);
    final Parameter param =  params.get(0);
    assertEquals(param.getName(), "skip");
}
 
Example 5
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test(description = "resolve top-level parameters")
public void testSharedSwaggerParametersTest() {
    final OpenAPI swagger = new OpenAPI();
    List<Parameter> parameters = new ArrayList<>();
    parameters.add(0,new Parameter().$ref("username"));
    swagger.path("/fun", new PathItem()
            .get(new Operation()
                    .parameters(parameters)
                    .responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("ok!")))));

    swagger.components(new Components().addParameters("username", new QueryParameter()
            .name("username")
            .schema(new StringSchema())));

    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    assertTrue(resolved.getComponents().getParameters().size() == 1);
    assertTrue(resolved.getPaths().get("/fun").getGet().getParameters().size() == 1);
}
 
Example 6
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test(description = "resolve top-level responses")
public void testSharedResponses() {
    final OpenAPI swagger = new OpenAPI();
    List<Parameter> parameters = new ArrayList<>();
    parameters.add(0,new Parameter().$ref("username"));
    swagger.path("/fun", new PathItem()
            .get(new Operation()
                    .parameters(parameters)
                    .responses(new ApiResponses().addApiResponse("200", new ApiResponse().$ref("#/components/responses/foo")))));

    swagger.components(new Components().addResponses("foo", new ApiResponse().description("ok!")));

    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    ApiResponse response = resolved.getPaths().get("/fun").getGet().getResponses().get("200");
    assertTrue(response.getDescription().equals("ok!"));
    assertTrue(response instanceof ApiResponse);
}
 
Example 7
Source File: ResolverCacheTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadInternalResponseRefWithSpaces(@Injectable ApiResponse mockedResponse) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addResponses("foo bar", mockedResponse));

    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    ApiResponse actualResult = cache.loadRef("#/components/responses/foo bar", RefFormat.INTERNAL, ApiResponse.class);
    assertEquals(actualResult, mockedResponse);
}
 
Example 8
Source File: Swagger3RestDocGenerator.java    From RestDoc with Apache License 2.0 5 votes vote down vote up
private Schema getOrGenerateComplexSchema(List<String> enums, Type type, List<PropertyModel> children, OpenAPI openAPI) {
    var componentName = ClassNameUtils.getComponentName(_config.getTypeInspector(), _config.getTypeNameParser(), type);

    if (openAPI.getComponents() == null)
        openAPI.components(new Components());
    if (openAPI.getComponents().getSchemas() == null)
        openAPI.getComponents().schemas(new LinkedHashMap<>());
    if (!openAPI.getComponents().getSchemas().containsKey(componentName)) {
        var schema = generateComplexTypeSchema(enums, type, children, openAPI);
        schema.setRequired(children.stream().filter(o -> o.isRequired()).map(o -> o.getName()).collect(Collectors.toList()));
        openAPI.getComponents().addSchemas(componentName, schema);
    }
    return openAPI.getComponents().getSchemas().get(componentName);
}
 
Example 9
Source File: ResolverCacheTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadInternalResponseRef(@Injectable ApiResponse mockedResponse) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addResponses("foo", mockedResponse));

    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    ApiResponse actualResult = cache.loadRef("#/components/responses/foo", RefFormat.INTERNAL, ApiResponse.class);
    assertEquals(actualResult, mockedResponse);

    assertNull(cache.loadRef("#/components/responses/bar", RefFormat.INTERNAL, ApiResponse.class));
}
 
Example 10
Source File: ResolverCacheTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadInternalDefinitionRefWithEscapedCharacters(@Injectable Schema mockedModel) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addSchemas("foo~bar/baz~1", mockedModel));

    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    Schema actualResult = cache.loadRef("#/components/schemas/foo~0bar~1baz~01", RefFormat.INTERNAL, Schema.class);
    assertEquals(actualResult, mockedModel);
}
 
Example 11
Source File: ResolverCacheTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadInternalDefinitionRefWithSpaces(@Injectable Schema mockedModel) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addSchemas("foo bar", mockedModel));

    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    Schema actualResult = cache.loadRef("#/components/schemas/foo bar", RefFormat.INTERNAL, Schema.class);
    assertEquals(actualResult, mockedModel);
}
 
Example 12
Source File: ResolverCacheTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadInternalDefinitionRef(@Injectable Schema mockedModel) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addSchemas("foo", mockedModel));

    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    Schema actualResult = cache.loadRef("#/components/schemas/foo", RefFormat.INTERNAL, Schema.class);
    assertEquals(actualResult, mockedModel);

    assertNull(cache.loadRef("#/components/schemas/bar", RefFormat.INTERNAL, Schema.class));
    assertNull(cache.loadRef("#/defs/bar", RefFormat.INTERNAL, Schema.class));
}
 
Example 13
Source File: ResolverCacheTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadInternalParameterRefWithSpaces(@Injectable Parameter mockedParameter) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addParameters("foo bar", mockedParameter));

    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    Parameter actualResult = cache.loadRef("#/components/parameters/foo bar", RefFormat.INTERNAL, Parameter.class);
    assertEquals(actualResult, mockedParameter);
}
 
Example 14
Source File: ResolverCacheTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadInternalParameterRef(@Injectable Parameter mockedParameter) throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addParameters("foo", mockedParameter));

    ResolverCache cache = new ResolverCache(openAPI, auths, null);
    Parameter actualResult = cache.loadRef("#/components/parameters/foo", RefFormat.INTERNAL, Parameter.class);
    assertEquals(actualResult, mockedParameter);

    assertNull(cache.loadRef("#/components/parameters/bar", RefFormat.INTERNAL, Parameter.class));
    assertNull(cache.loadRef("#/params/foo", RefFormat.INTERNAL, Parameter.class));
}
 
Example 15
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
private void testMapRemoteModelProperty(String remoteRef) {
    final OpenAPI swagger = new OpenAPI();
    swagger.components(new Components().addSchemas("Sample", new Schema()
            .addProperties("remoteRef", new Schema().additionalProperties(new Schema().$ref(remoteRef)))));

    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final Schema prop = (Schema)resolved.getComponents().getSchemas().get("Sample").getProperties().get("remoteRef");
    assertTrue(prop.getAdditionalProperties() != null);


    assertEquals(((Schema) prop.getAdditionalProperties()).get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
 
Example 16
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
private void testArrayRemoteModelProperty(String remoteRef) {

        final OpenAPI swagger = new OpenAPI();
        swagger.components(new Components().addSchemas("Sample", new Schema()
                .addProperties("remoteRef", new ArraySchema().items(new Schema().$ref(remoteRef)))));

        final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
        final Schema prop = (Schema) resolved.getComponents().getSchemas().get("Sample").getProperties().get("remoteRef");
        assertTrue(prop instanceof ArraySchema);
        final ArraySchema ap = (ArraySchema) prop;
        assertEquals(ap.getItems().get$ref(), "#/components/schemas/Tag");
        assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
    }
 
Example 17
Source File: OpenAPIResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
private void testSimpleRemoteModelProperty(String remoteRef) {
    final OpenAPI swagger = new OpenAPI();
    swagger.components(new Components().addSchemas("Sample", new Schema()
            .addProperties("remoteRef", new Schema().$ref(remoteRef))));


    final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
    final Schema prop = (Schema) resolved.getComponents().getSchemas().get("Sample").getProperties().get("remoteRef");
    assertTrue(prop.get$ref() != null);

    assertEquals(prop.get$ref(), "#/components/schemas/Tag");
    assertNotNull(swagger.getComponents().getSchemas().get("Tag"));
}
 
Example 18
Source File: SpringDocTestApp.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
@Bean
public OpenAPI defineOpenApi() {
	OpenAPI api = new OpenAPI();
	api.components(new Components().addResponses("Unauthorized",
			new ApiResponse().description("Unauthorized")
					.content(new Content().addMediaType(MediaType.APPLICATION_JSON_VALUE,
							new io.swagger.v3.oas.models.media.MediaType().schema(new StringSchema())))));
	return api;
}
 
Example 19
Source File: SpringDocTestApp.java    From springdoc-openapi with Apache License 2.0 5 votes vote down vote up
@Bean
public OpenAPI defineOpenApi() {
	OpenAPI api = new OpenAPI();
	api.components(new Components().addResponses("Unauthorized",
			new ApiResponse().description("Unauthorized")
					.content(new Content().addMediaType(MediaType.APPLICATION_JSON_VALUE,
							new io.swagger.v3.oas.models.media.MediaType().schema(new StringSchema())))));
	return api;
}
 
Example 20
Source File: ComponentsProcessorTest.java    From swagger-parser with Apache License 2.0 3 votes vote down vote up
@Test
public void testComponentsSchemasProcessor(@Injectable final Schema model1,
                                     @Injectable final Schema model2,
                                     @Injectable final ResolverCache cache) throws Exception {

    final OpenAPI openAPI = new OpenAPI();
    openAPI.components(new Components().addSchemas("foo", model1));
    openAPI.getComponents().addSchemas("bar", model2);



    new Expectations() {{

        new SchemaProcessor(cache, openAPI);
        times = 1;
        result = schemaProcessor;


        schemaProcessor.processSchema((Schema) any);
        times = 2;
    }};

    new ComponentsProcessor(openAPI,cache).processComponents();



    new Verifications() {{
        schemaProcessor.processSchema(model1);
        schemaProcessor.processSchema(model2);
    }};
}