Java Code Examples for io.swagger.v3.oas.models.OpenAPI#path()
The following examples show how to use
io.swagger.v3.oas.models.OpenAPI#path() .
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: DefaultCodegenTest.java From openapi-generator with Apache License 2.0 | 6 votes |
@Test public void testDefaultResponseShouldBeLast() { OpenAPI openAPI = TestUtils.createOpenAPI(); Operation myOperation = new Operation().operationId("myOperation").responses( new ApiResponses() .addApiResponse( "default", new ApiResponse().description("Default")) .addApiResponse( "422", new ApiResponse().description("Error")) ); openAPI.path("/here", new PathItem().get(myOperation)); final DefaultCodegen codegen = new DefaultCodegen(); codegen.setOpenAPI(openAPI); CodegenOperation co = codegen.fromOperation("/here", "get", myOperation, null); Assert.assertEquals(co.responses.get(0).message, "Error"); Assert.assertEquals(co.responses.get(1).message, "Default"); }
Example 2
Source File: OpenAPIResolverTest.java From swagger-parser with Apache License 2.0 | 6 votes |
@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 3
Source File: OpenAPIResolverTest.java From swagger-parser with Apache License 2.0 | 6 votes |
@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: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 6 votes |
@Test public void testArbitraryObjectResponseMapInline() { OpenAPI openAPI = new OpenAPI(); Schema schema = new Schema(); schema.setAdditionalProperties(new ObjectSchema()); openAPI.path("/foo/baz", new PathItem() .get(new Operation() .responses(new ApiResponses().addApiResponse("200", new ApiResponse() .description("it works!") .content(new Content().addMediaType("*/*", new MediaType().schema(schema))))))); new InlineModelResolver().flatten(openAPI); ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200"); Schema property = response.getContent().get("*/*").getSchema(); assertTrue(property.getAdditionalProperties() != null); assertTrue(property.getAdditionalProperties() instanceof Schema); assertTrue(openAPI.getComponents().getSchemas() == null); Schema inlineProp = (Schema)property.getAdditionalProperties(); assertTrue(inlineProp instanceof ObjectSchema); ObjectSchema op = (ObjectSchema) inlineProp; assertNull(op.getProperties()); }
Example 5
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 6 votes |
@Test public void testBasicInput() { OpenAPI openAPI = new OpenAPI(); openAPI.setComponents(new Components()); Schema user = new Schema(); user.addProperties("name", new StringSchema()); openAPI.path("/foo/baz", new PathItem() .post(new Operation() .requestBody(new RequestBody() .content(new Content().addMediaType("*/*",new MediaType().schema(new Schema().$ref("User"))))))); openAPI.getComponents().addSchemas("User", user); new InlineModelResolver().flatten(openAPI); }
Example 6
Source File: OpenAPIResolverTest.java From swagger-parser with Apache License 2.0 | 6 votes |
@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 7
Source File: OpenAPIResolverTest.java From swagger-parser with Apache License 2.0 | 6 votes |
@Test(description = "resolve array response remote refs in yaml") public void testYamlArrayResponseRemoteRefs() { final OpenAPI swagger = new OpenAPI(); swagger.path("/fun", new PathItem() .get(new Operation() .responses(new ApiResponses().addApiResponse("200", new ApiResponse() .content(new Content().addMediaType("*/*",new MediaType().schema( new ArraySchema().items( new Schema().$ref(replacePort(REMOTE_REF_YAML)))))))))); final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve(); final ApiResponse response = swagger.getPaths().get("/fun").getGet().getResponses().get("200"); final ArraySchema array = (ArraySchema) response.getContent().get("*/*").getSchema(); assertNotNull(array.getItems()); assertEquals(array.getItems().get$ref(), "#/components/schemas/Tag"); assertNotNull(swagger.getComponents().getSchemas().get("Tag")); }
Example 8
Source File: OpenAPIResolverTest.java From swagger-parser with Apache License 2.0 | 6 votes |
@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 9
Source File: DefaultCodegenTest.java From openapi-generator with Apache License 2.0 | 6 votes |
@Test public void testResponseWithNoSchemaInHeaders() { OpenAPI openAPI = TestUtils.createOpenAPI(); ApiResponse response2XX = new ApiResponse() .description("OK") .addHeaderObject("x-custom-header", new Header() .description("a custom header") .style(Header.StyleEnum.SIMPLE)); Operation operation1 = new Operation().operationId("op1").responses(new ApiResponses().addApiResponse("2XX", response2XX)); openAPI.path("/here", new PathItem().get(operation1)); final DefaultCodegen codegen = new DefaultCodegen(); codegen.setOpenAPI(openAPI); CodegenResponse cr = codegen.fromResponse("2XX", response2XX); Assert.assertNotNull(cr); Assert.assertTrue(cr.hasHeaders); }
Example 10
Source File: OpenAPIResolverTest.java From swagger-parser with Apache License 2.0 | 5 votes |
@Test(description = "resolve shared path parameters") public void testSharedPathParametersTest() { final OpenAPI swagger = new OpenAPI(); Operation operation = new Operation() .responses(new ApiResponses().addApiResponse("200", new ApiResponse().description("ok!"))); PathItem path = new PathItem().get(operation); path.addParametersItem(new QueryParameter() .name("username") .schema(new StringSchema())); swagger.path("/fun", path); final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve(); assertNull(resolved.getPaths().get("/fun").getParameters()); assertTrue(resolved.getPaths().get("/fun").getGet().getParameters().size() == 1); }
Example 11
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 5 votes |
@Test public void resolveInlineRequestBody() throws Exception { OpenAPI openAPI = new OpenAPI(); ObjectSchema objectSchema = new ObjectSchema(); objectSchema.addProperties("street", new StringSchema()); Schema schema = new Schema(); schema.addProperties("address", objectSchema); schema.addProperties("name", new StringSchema()); MediaType mediaType = new MediaType(); mediaType.setSchema(schema); Content content = new Content(); content.addMediaType("*/*", mediaType ); RequestBody requestBody = new RequestBody(); requestBody.setContent(content); Operation operation = new Operation(); operation.setRequestBody(requestBody); PathItem pathItem = new PathItem(); pathItem.setGet(operation); openAPI.path("/hello",pathItem); new InlineModelResolver().flatten(openAPI); Operation getOperation = openAPI.getPaths().get("/hello").getGet(); RequestBody body = getOperation.getRequestBody(); assertTrue(body.getContent().get("*/*").getSchema().get$ref() != null); Schema bodySchema = openAPI.getComponents().getSchemas().get("body"); assertTrue(bodySchema instanceof Schema); assertNotNull(bodySchema.getProperties().get("address")); }
Example 12
Source File: OpenAPIResolverTest.java From swagger-parser with Apache License 2.0 | 5 votes |
private void testResponseRemoteRefs(String remoteRef) { final OpenAPI swagger = new OpenAPI(); swagger.path("/fun", new PathItem() .get(new Operation() .responses(new ApiResponses().addApiResponse( "200", new ApiResponse() .content(new Content().addMediaType("*/*",new MediaType().schema(new Schema().$ref(remoteRef)))))))); final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve(); final ApiResponse response = swagger.getPaths().get("/fun").getGet().getResponses().get("200"); final Schema ref = response.getContent().get("*/*").getSchema(); assertEquals(ref.get$ref(), "#/components/schemas/Tag"); assertNotNull(swagger.getComponents().getSchemas().get("Tag")); }
Example 13
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 5 votes |
@Test public void testInlineMapResponse() throws Exception { OpenAPI openAPI = new OpenAPI(); Schema schema = new Schema(); schema.setAdditionalProperties(new StringSchema()); schema.addExtension("x-ext", "ext-prop"); ApiResponse apiResponse = new ApiResponse(); apiResponse.description("it works!"); MediaType mediaType = new MediaType(); mediaType.setSchema(schema); Content content = new Content(); content.addMediaType("*/*",mediaType); apiResponse.setContent(content); apiResponse.addExtension("x-foo", "bar"); ApiResponses apiResponses = new ApiResponses(); apiResponses.addApiResponse("200",apiResponse); openAPI.path("/foo/baz", new PathItem() .get(new Operation() .responses(apiResponses))); new InlineModelResolver().flatten(openAPI); ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200"); Schema property = response.getContent().get("*/*").getSchema(); assertTrue(property.getAdditionalProperties() != null); assertTrue(openAPI.getComponents().getSchemas() == null); assertEquals(1, property.getExtensions().size()); assertEquals("ext-prop", property.getExtensions().get("x-ext")); }
Example 14
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 5 votes |
@Test public void testArrayResponse() { OpenAPI openAPI = new OpenAPI(); ObjectSchema objectSchema = new ObjectSchema(); objectSchema.addProperties("name", new StringSchema()); ArraySchema schema = new ArraySchema(); schema.setItems(objectSchema); ApiResponse apiResponse = new ApiResponse(); apiResponse.addExtension("x-foo", "bar"); apiResponse.setDescription("it works!"); apiResponse.setContent(new Content().addMediaType("*/*", new MediaType().schema(schema))); openAPI.path("/foo/baz", new PathItem() .get(new Operation() .responses(new ApiResponses().addApiResponse("200", apiResponse)))); new InlineModelResolver().flatten(openAPI); ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200"); assertTrue(response.getContent().get("*/*").getSchema() instanceof ArraySchema); ArraySchema am = (ArraySchema) response.getContent().get("*/*").getSchema(); Schema items = am.getItems(); assertTrue(items.get$ref() != null); assertEquals(items.get$ref(), "#/components/schemas/inline_response_200"); Schema inline = openAPI.getComponents().getSchemas().get("inline_response_200"); assertTrue(inline instanceof Schema); assertNotNull(inline.getProperties().get("name")); assertTrue(inline.getProperties().get("name") instanceof StringSchema); }
Example 15
Source File: TypeScriptAngularClientCodegenTest.java From openapi-generator with Apache License 2.0 | 5 votes |
@Test public void testOperationIdParser() { OpenAPI openAPI = TestUtils.createOpenAPI(); Operation operation1 = new Operation().operationId("123_test_@#$%_special_tags").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK"))); openAPI.path("another-fake/dummy/", new PathItem().get(operation1)); final TypeScriptAngularClientCodegen codegen = new TypeScriptAngularClientCodegen(); codegen.setOpenAPI(openAPI); CodegenOperation co1 = codegen.fromOperation("/another-fake/dummy/", "get", operation1, null); org.testng.Assert.assertEquals(co1.operationId, "_123testSpecialTags"); }
Example 16
Source File: OpenAPIResolverTest.java From swagger-parser with Apache License 2.0 | 5 votes |
private void testOperationBodyparamRemoteRefs(String remoteRef) { final OpenAPI swagger = new OpenAPI(); swagger.path("/fun", new PathItem() .get(new Operation() .requestBody(new RequestBody() .content(new Content().addMediaType("*/*",new MediaType() .schema(new Schema().$ref(remoteRef))))))); final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve(); final RequestBody param = swagger.getPaths().get("/fun").getGet().getRequestBody(); final Schema ref = param.getContent().get("*/*").getSchema(); assertEquals(ref.get$ref(), "#/components/schemas/Tag"); assertNotNull(swagger.getComponents().getSchemas().get("Tag")); }
Example 17
Source File: DefaultCodegenTest.java From openapi-generator with Apache License 2.0 | 5 votes |
@Test public void testLeadingSlashIsAddedIfMissing() { OpenAPI openAPI = TestUtils.createOpenAPI(); Operation operation1 = new Operation().operationId("op1").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK"))); openAPI.path("/here", new PathItem().get(operation1)); Operation operation2 = new Operation().operationId("op2").responses(new ApiResponses().addApiResponse("201", new ApiResponse().description("OK"))); openAPI.path("some/path", new PathItem().get(operation2)); final DefaultCodegen codegen = new DefaultCodegen(); codegen.setOpenAPI(openAPI); CodegenOperation co1 = codegen.fromOperation("/here", "get", operation2, null); Assert.assertEquals(co1.path, "/here"); CodegenOperation co2 = codegen.fromOperation("some/path", "get", operation2, null); Assert.assertEquals(co2.path, "/some/path"); }
Example 18
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 4 votes |
@Test public void resolveInlineArrayResponseWithTitle() throws Exception { OpenAPI openAPI = new OpenAPI(); ApiResponse apiResponse = new ApiResponse(); apiResponse.addExtension("x-foo", "bar"); apiResponse.description("it works!"); Map<String,Schema> properties = new HashMap<>(); properties.put("name", new StringSchema()); apiResponse.content(new Content().addMediaType("*/*", new MediaType().schema(new ArraySchema() .items(new ObjectSchema() .title("FooBar") .properties(properties))))); openAPI.path("/foo/baz", new PathItem() .get(new Operation() .responses(new ApiResponses().addApiResponse("200",apiResponse)))); new InlineModelResolver().flatten(openAPI); ApiResponse response = openAPI.getPaths().get("/foo/baz").getGet().getResponses().get("200"); assertNotNull(response); assertNotNull(response.getContent().get("*/*").getSchema()); Schema responseProperty = response.getContent().get("*/*").getSchema(); // no need to flatten more assertTrue(responseProperty instanceof ArraySchema); ArraySchema ap = (ArraySchema) responseProperty; Schema p = ap.getItems(); assertNotNull(p); assertEquals(p.get$ref(), "#/components/schemas/"+ "FooBar"); Schema inline = openAPI.getComponents().getSchemas().get("FooBar"); assertNotNull(inline); assertTrue(inline instanceof Schema); assertNotNull(inline.getProperties().get("name")); assertTrue(inline.getProperties().get("name") instanceof StringSchema); }
Example 19
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 3 votes |
@Test public void resolveInlineParameter() throws Exception { OpenAPI openAPI = new OpenAPI(); ObjectSchema objectSchema = new ObjectSchema(); objectSchema.addProperties("street", new StringSchema()); Schema schema = new Schema(); schema.addProperties("address", objectSchema); schema.addProperties("name", new StringSchema()); Parameter parameter = new Parameter(); parameter.setName("name"); parameter.setSchema(schema); List parameters = new ArrayList(); parameters.add(parameter); Operation operation = new Operation(); operation.setParameters(parameters); PathItem pathItem = new PathItem(); pathItem.setGet(operation); openAPI.path("/hello",pathItem); new InlineModelResolver().flatten(openAPI); Operation getOperation = openAPI.getPaths().get("/hello").getGet(); Parameter param = getOperation.getParameters().get(0); assertTrue(param.getSchema().get$ref() != null); Schema bodySchema = openAPI.getComponents().getSchemas().get("name"); assertTrue(bodySchema instanceof Schema); assertNotNull(bodySchema.getProperties().get("address")); }
Example 20
Source File: InlineModelResolverTest.java From swagger-parser with Apache License 2.0 | 2 votes |
@Test public void testInlineResponseModelWithTitle() throws Exception { OpenAPI openAPI = new OpenAPI(); String responseTitle = "GetBarResponse"; StringSchema stringSchema1 = new StringSchema(); ObjectSchema objectSchema1 = new ObjectSchema(); objectSchema1.setTitle(responseTitle); objectSchema1.addProperties("name", stringSchema1); MediaType mediaType1 = new MediaType(); mediaType1.setSchema(objectSchema1); Content content1 = new Content(); content1.addMediaType("*/*", mediaType1 ); ApiResponse response1= new ApiResponse(); response1.setDescription("it works!"); response1.setContent(content1); ApiResponses responses1 = new ApiResponses(); responses1.addApiResponse("200",response1); Operation operation1 = new Operation(); operation1.setResponses(responses1); PathItem pathItem1 = new PathItem(); pathItem1.setGet(operation1); openAPI.path("/foo/bar",pathItem1); StringSchema stringSchema2 = new StringSchema(); ObjectSchema objectSchema2 = new ObjectSchema(); objectSchema2.addProperties("name", stringSchema2); objectSchema2.addExtension("x-foo", "bar"); MediaType mediaType2 = new MediaType(); mediaType2.setSchema(objectSchema2); Content content2 = new Content(); content2.addMediaType("*/*", mediaType2 ); ApiResponse response2 = new ApiResponse(); response2.setDescription("it works!"); response2.setContent(content2); ApiResponses responses2 = new ApiResponses(); responses2.addApiResponse("200",response2); Operation operation2 = new Operation(); operation2.setResponses(responses2); PathItem pathItem2 = new PathItem(); pathItem2.setGet(operation2); openAPI.path("/foo/baz",pathItem2); new InlineModelResolver().flatten(openAPI); Map<String, ApiResponse> responses = openAPI.getPaths().get("/foo/bar").getGet().getResponses(); ApiResponse response = responses.get("200"); assertNotNull(response); assertTrue(response.getContent().get("*/*").getSchema().get$ref() != null ); Schema model = openAPI.getComponents().getSchemas().get(responseTitle); assertTrue(model.getProperties().size() == 1); assertNotNull(model.getProperties().get("name")); assertTrue(model.getProperties().get("name") instanceof StringSchema); }