Search in sources :

Example 21 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testIssue915.

@Test
public void testIssue915() {
    ParseOptions parseOptions = new ParseOptions();
    parseOptions.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().read("src/test/resources/issue_918.yaml", null, parseOptions);
    Map<String, Header> headers = openAPI.getPaths().get("/2.0/users/").getGet().getResponses().get("200").getHeaders();
    String description = headers.get("X-Rate-Limit").getDescription();
    assertEquals(description, "The number of allowed requests in the current period");
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 22 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testIssue1177.

@Test
public void testIssue1177(@Injectable final List<AuthorizationValue> auths) {
    String path = "/issue-1177/swagger.yaml";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    options.setResolveFully(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, auths, options).getOpenAPI();
    // $ref response with $ref header
    ApiResponse petsListApiResponse = openAPI.getPaths().get("/update-pets").getPost().getResponses().get("200");
    assertNotNull(petsListApiResponse);
    Header sessionIdHeader = petsListApiResponse.getHeaders().get("x-session-id");
    assertNotNull(sessionIdHeader);
    Schema petsListSchema = openAPI.getComponents().getSchemas().get("PetsList");
    assertNotNull(petsListSchema);
    assertNotNull(openAPI.getComponents().getHeaders());
    Header sessionIdHeaderComponent = openAPI.getComponents().getHeaders().get("x-session-id");
    assertTrue(sessionIdHeader == sessionIdHeaderComponent);
    assertTrue(petsListApiResponse.getContent().get("application/json").getSchema() == petsListSchema);
}
Also used : Header(io.swagger.v3.oas.models.headers.Header) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) Test(org.testng.annotations.Test)

Example 23 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testValidateExternalRefsTrue.

@Test(description = "option true, adds Original Location to messages when ref is relative/local")
public void testValidateExternalRefsTrue() {
    ParseOptions options = new ParseOptions();
    options.setValidateExternalRefs(true);
    options.setResolve(true);
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("./swos-443/root.yaml", null, options);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(openAPI);
    assertNotNull(result.getMessages());
    assertEquals(result.getMessages().size(), 19);
    assertTrue(result.getMessages().contains("attribute components.requestBodies.NewItem.asdasd is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.requestBodies.NewItem.descasdasdription is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.responses.GeneralError.descrsaiption is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.responses.GeneralError.asdas is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.responses.GeneralError.description is missing (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.schemas.Examples.nonExpected is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.parameters.skipParam.[skip].in is not of type `[query|header|path|cookie]` (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.securitySchemes.api_key.namex is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.securitySchemes.api_key.name is missing (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.callbacks.webhookVerificationEvent.postx is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.headers.X-Rate-Limit-Limit.descriptasdd is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.links.unsubscribe.parametersx is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.examples.response-example.summaryx is unexpected (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.examples.response-example. value and externalValue are both present (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.callbacks.failed.wrongField is not of type `object` (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute paths.~1refPet(get).responses is missing (./ref.yaml)"));
    // error message in main file
    assertTrue(result.getMessages().contains("attribute components.schemas.InvalidSchema.invalid is unexpected"));
    assertTrue(result.getMessages().contains("attribute components.schemas.ErrorModel.properties is not of type `object` (./ref.yaml)"));
    assertTrue(result.getMessages().contains("attribute components.schemas.Examples.properties is not of type `object` (./ref.yaml)"));
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 24 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method doRelativeFileTest.

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");
    // we successfully converted the RefPath to a Path
    assertEquals(path.getClass(), PathItem.class);
    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_1");
    return openAPI;
}
Also used : ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) MapSchema(io.swagger.v3.oas.models.media.MapSchema) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) ApiResponse(io.swagger.v3.oas.models.responses.ApiResponse) PathItem(io.swagger.v3.oas.models.PathItem) ByteArraySchema(io.swagger.v3.oas.models.media.ByteArraySchema) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) HeaderParameter(io.swagger.v3.oas.models.parameters.HeaderParameter) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) OpenAPI(io.swagger.v3.oas.models.OpenAPI)

Example 25 with Header

use of io.swagger.v3.oas.annotations.headers.Header in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testIssue1644_EmptyValue.

@Test
public void testIssue1644_EmptyValue() throws Exception {
    ParseOptions options = new ParseOptions();
    String issue1644 = "openapi: 3.0.0\n" + "info:\n" + "  title: Operations\n" + "  version: 0.0.0\n" + "paths:\n" + "  \"/operations\":\n" + "    post:\n" + "      parameters:\n" + "        - name: param0\n" + "          in: ''\n" + "          schema:\n" + "            type: string\n" + "      responses:\n" + "        default:\n" + "          description: None\n";
    SwaggerParseResult result = new OpenAPIV3Parser().readContents(issue1644, null, options);
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.getOpenAPI());
    assertEquals(result.getMessages().size(), 1);
    assertTrue(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].in is not of type `[query|header|path|cookie]`"));
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)32 OpenAPI (io.swagger.v3.oas.models.OpenAPI)26 Header (io.swagger.v3.oas.models.headers.Header)25 Parameter (io.swagger.v3.oas.models.parameters.Parameter)16 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)13 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)11 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)11 Schema (io.swagger.v3.oas.models.media.Schema)10 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)10 MediaType (io.swagger.v3.oas.models.media.MediaType)8 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)8 Components (io.swagger.v3.oas.models.Components)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 StringSchema (io.swagger.v3.oas.models.media.StringSchema)6 AuthorizationValue (io.swagger.v3.parser.core.models.AuthorizationValue)6 PathItem (io.swagger.v3.oas.models.PathItem)5 Example (io.swagger.v3.oas.models.examples.Example)5 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)5 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)5 RefFormat (io.swagger.v3.parser.models.RefFormat)5