Java Code Examples for io.swagger.v3.oas.models.PathItem#getGet()

The following examples show how to use io.swagger.v3.oas.models.PathItem#getGet() . 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: OperationBuilder.java    From springdoc-openapi with Apache License 2.0 6 votes vote down vote up
/**
 * Extract operation id from path item set.
 *
 * @param path the path
 * @return the set
 */
private Set<String> extractOperationIdFromPathItem(PathItem path) {
	Set<String> ids = new HashSet<>();
	if (path.getGet() != null && StringUtils.isNotBlank(path.getGet().getOperationId())) {
		ids.add(path.getGet().getOperationId());
	}
	if (path.getPost() != null && StringUtils.isNotBlank(path.getPost().getOperationId())) {
		ids.add(path.getPost().getOperationId());
	}
	if (path.getPut() != null && StringUtils.isNotBlank(path.getPut().getOperationId())) {
		ids.add(path.getPut().getOperationId());
	}
	if (path.getDelete() != null && StringUtils.isNotBlank(path.getDelete().getOperationId())) {
		ids.add(path.getDelete().getOperationId());
	}
	if (path.getOptions() != null && StringUtils.isNotBlank(path.getOptions().getOperationId())) {
		ids.add(path.getOptions().getOperationId());
	}
	if (path.getHead() != null && StringUtils.isNotBlank(path.getHead().getOperationId())) {
		ids.add(path.getHead().getOperationId());
	}
	if (path.getPatch() != null && StringUtils.isNotBlank(path.getPatch().getOperationId())) {
		ids.add(path.getPatch().getOperationId());
	}
	return ids;
}
 
Example 2
Source File: RequestParameterTest.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Return the list of parameters given the path item (an endpoint)
 *
 * @param pathItem Endpoint defined as a PathItem object
 * @return set of parameters for the specified endpoint
 */
public static Set<String> parseEndpoint(PathItem pathItem) throws IllegalArgumentException {
  List<Parameter> parameterList;
  Set<String> parameterSet = new TreeSet<>();
  if (pathItem.getGet() != null) {
    parameterList = pathItem.getGet().getParameters();
  } else if (pathItem.getPost() != null) {
    parameterList = pathItem.getPost().getParameters();
  } else {
    throw new IllegalArgumentException("Schema Parser does not support HTTP methods other than GET/POST");
  }

  for (Parameter parameter : parameterList) {
    Assert.assertFalse(parameterSet.contains(parameter.getName()));
    parameterSet.add(parameter.getName());
  }
  return parameterSet;
}
 
Example 3
Source File: ResponseTest.java    From cruise-control with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Check the consistency for all POST and GET endpoints defined in OpenAPI spec.
 */
@Test
public void checkOpenAPISpec() {
  ParseOptions options = new ParseOptions();
  options.setResolve(true);
  options.setFlatten(true);
  _openAPI = new OpenAPIV3Parser().read(OPENAPI_SPEC_PATH, null, options);
  for (PathItem path : _openAPI.getPaths().values()) {
    if (path.getGet() != null) {
      checkOperation(path.getGet());
    }
    if (path.getPost() != null) {
      checkOperation(path.getPost());
    }
  }
}
 
Example 4
Source File: OperationHelper.java    From zap-extensions with Apache License 2.0 5 votes vote down vote up
public List<OperationModel> getAllOperations(PathItem path, String url) {
    List<OperationModel> operations = new LinkedList<OperationModel>();

    if (path.getGet() != null) {
        operations.add(new OperationModel(url, path.getGet(), RequestMethod.GET));
    }
    if (path.getPost() != null) {
        operations.add(new OperationModel(url, path.getPost(), RequestMethod.POST));
    }
    if (path.getPut() != null) {
        operations.add(new OperationModel(url, path.getPut(), RequestMethod.PUT));
    }
    if (path.getHead() != null) {
        operations.add(new OperationModel(url, path.getHead(), RequestMethod.HEAD));
    }
    if (path.getOptions() != null) {
        operations.add(new OperationModel(url, path.getOptions(), RequestMethod.OPTION));
    }
    if (path.getDelete() != null) {
        operations.add(new OperationModel(url, path.getDelete(), RequestMethod.DELETE));
    }
    if (path.getPatch() != null) {
        operations.add(new OperationModel(url, path.getPatch(), RequestMethod.PATCH));
    }
    if (operations.isEmpty()) {
        log.debug("Failed to find any operations for url=" + url + " path=" + path);
    }

    return operations;
}
 
Example 5
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testPathsWithRefResponse() {
    String json = "{\n" +
            "  \"openapi\": \"3.0.0\",\n" +
            "  \"paths\": {\n" +
            "    \"/pet\": {\n" +
            "      \"get\": {\n" +
            "        \"responses\": {\n" +
            "          \"200\": {\n" +
            "            \"$ref\": \"#/components/responses/OK\"" +
            "          }\n" +
            "        }\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();

    SwaggerParseResult result = parser.readContents(json, null, null);
    OpenAPI openAPI = result.getOpenAPI();

    PathItem path = openAPI.getPaths().get("/pet");
    assertNotNull(path);
    Operation operation = path.getGet();
    assertNotNull(operation);
    assertTrue(operation.getResponses().containsKey("200"));
    assertEquals(ApiResponse.class,operation.getResponses().get("200").getClass());
    ApiResponse refResponse = operation.getResponses().get("200");
    assertEquals("#/components/responses/OK",refResponse.get$ref());
}
 
Example 6
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void testPaths() {
    String json = "{\n" +
            "  \"openapi\": \"3.0.0\",\n" +
            "  \"paths\": {\n" +
            "    \"/pet\": {\n" +
            "      \"foo\": \"bar\",\n" +
            "      \"get\": {\n" +
            "        \"security\": [\n" +
            "          {\n" +
            "            \"petstore_auth\": [\n" +
            "              \"write:pets\",\n" +
            "              \"read:pets\"\n" +
            "            ]\n" +
            "          }\n" +
            "        ]\n" +
            "      }\n" +
            "    }\n" +
            "  }\n" +
            "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();

    SwaggerParseResult result = parser.readContents(json, null, null);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);

    assertTrue(messages.contains("attribute paths.'/pet'.foo is unexpected"));
    OpenAPI openAPI = result.getOpenAPI();

    PathItem path = openAPI.getPaths().get("/pet");
    assertNotNull(path);
    Operation operation = path.getGet();
    assertNotNull(operation);
    List<SecurityRequirement> security = operation.getSecurity();

    assertTrue(security.size() == 1);
    Map<String, List<String>> requirement = security.get(0);

    assertTrue(requirement.containsKey("petstore_auth"));
    List<String> scopesList = requirement.get("petstore_auth");

    Set<String> scopes = new HashSet<>(scopesList);
    assertTrue(scopes.contains("read:pets"));
    assertTrue(scopes.contains("write:pets"));
}
 
Example 7
Source File: OpenAPIDeserializerTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test(dataProvider = "data")
public void readProducesTestEndpoint(JsonNode rootNode) throws Exception {
    final OpenAPIDeserializer deserializer = new OpenAPIDeserializer();
    final SwaggerParseResult result = deserializer.deserialize(rootNode);

    Assert.assertNotNull(result);

    final OpenAPI openAPI = result.getOpenAPI();
    Assert.assertNotNull(openAPI);

    final Paths paths = openAPI.getPaths();
    Assert.assertNotNull(paths);
    Assert.assertEquals(paths.size(), 18);

    //parameters operation get
    PathItem producesTestEndpoint = paths.get("/producesTest");
    Assert.assertNotNull(producesTestEndpoint.getGet());
    Assert.assertNotNull(producesTestEndpoint.getGet().getParameters());
    Assert.assertTrue(producesTestEndpoint.getGet().getParameters().isEmpty());

    Operation operation = producesTestEndpoint.getGet();
    ApiResponses responses = operation.getResponses();
    Assert.assertNotNull(responses);
    Assert.assertFalse(responses.isEmpty());

    ApiResponse response = responses.get("200");
    Assert.assertNotNull(response);
    Assert.assertEquals("it works", response.getDescription());

    Content content = response.getContent();
    Assert.assertNotNull(content);
    MediaType mediaType = content.get("application/json");
    Assert.assertNotNull(mediaType);

    Schema schema = mediaType.getSchema();
    Assert.assertNotNull(schema);
    Assert.assertTrue(schema instanceof ObjectSchema);

    ObjectSchema objectSchema = (ObjectSchema) schema;
    schema = objectSchema.getProperties().get("name");
    Assert.assertNotNull(schema);

    Assert.assertTrue(schema instanceof StringSchema);
}
 
Example 8
Source File: OpenAPIV3ParserTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
private OpenAPI doRelativeFileTest(String location) {
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult readResult = parser.readLocation(location, null, options);

    if (readResult.getMessages().size() > 0) {
        Json.prettyPrint(readResult.getMessages());
    }
    final OpenAPI openAPI = readResult.getOpenAPI();
    final PathItem path = openAPI.getPaths().get("/health");
    assertEquals(path.getClass(), PathItem.class); //we successfully converted the RefPath to a Path

    final List<Parameter> parameters = path.getParameters();
    assertParamDetails(parameters, 0, QueryParameter.class, "param1", "query");
    assertParamDetails(parameters, 1, HeaderParameter.class, "param2", "header");

    final Operation operation = path.getGet();
    final List<Parameter> operationParams = operation.getParameters();
    assertParamDetails(operationParams, 0, PathParameter.class, "param3", "path");
    assertParamDetails(operationParams, 1, HeaderParameter.class, "param4", "header");

    final Map<String, ApiResponse> responsesMap = operation.getResponses();

    assertResponse(openAPI, responsesMap, "200","application/json", "Health information from the server", "#/components/schemas/health");
    assertResponse(openAPI, responsesMap, "400","*/*", "Your request was not valid", "#/components/schemas/error");
    assertResponse(openAPI, responsesMap, "500","*/*", "An unexpected error occur during processing", "#/components/schemas/error");

    final Map<String, Schema> definitions = openAPI.getComponents().getSchemas();
    final Schema refInDefinitions = definitions.get("refInDefinitions");
    assertEquals(refInDefinitions.getDescription(), "The example model");
    expectedPropertiesInModel(refInDefinitions, "foo", "bar");

    final ArraySchema arrayModel = (ArraySchema) definitions.get("arrayModel");
    final Schema arrayModelItems = arrayModel.getItems();
    assertEquals(arrayModelItems.get$ref(), "#/components/schemas/foo");

    final Schema fooModel = definitions.get("foo");
    assertEquals(fooModel.getDescription(), "Just another model");
    expectedPropertiesInModel(fooModel, "hello", "world");

    final ComposedSchema composedCat = (ComposedSchema) definitions.get("composedCat");
    final Schema child =  composedCat.getAllOf().get(2);
    expectedPropertiesInModel(child, "huntingSkill", "prop2", "reflexes", "reflexMap");
    final ArraySchema reflexes = (ArraySchema) child.getProperties().get("reflexes");
    final Schema reflexItems = reflexes.getItems();
    assertEquals(reflexItems.get$ref(), "#/components/schemas/reflex");
    assertTrue(definitions.containsKey(reflexItems.get$ref().substring(reflexItems.get$ref().lastIndexOf("/")+1)));

    final Schema reflexMap = (Schema) child.getProperties().get("reflexMap");
    final Schema reflexMapAdditionalProperties = (Schema) reflexMap.getAdditionalProperties();
    assertEquals(reflexMapAdditionalProperties.get$ref(), "#/components/schemas/reflex");

    assertEquals(composedCat.getAllOf().size(), 3);
    assertEquals(composedCat.getAllOf().get(0).get$ref(), "#/components/schemas/pet");
    assertEquals(composedCat.getAllOf().get(1).get$ref(), "#/components/schemas/foo_2");

    return openAPI;
}