Search in sources :

Example 26 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method propertyTest.

@Test
public void propertyTest(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: 3.0.1\n" + "paths:\n" + "  /primitiveBody/inline:\n" + "    post:\n" + "      x-swagger-router-controller: TestController\n" + "      operationId: inlineRequiredBody\n" + "      requestBody:\n" + "        content:\n" + "          application/json:\n" + "            schema:\n" + "              type: object\n" + "              properties:\n" + "                name:\n" + "                  type: string\n" + "        required: true\n" + "      responses:\n" + "        '200':\n" + "          description: ok!";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readContents(yaml, auths, options);
    OpenAPI openAPI = result.getOpenAPI();
    Map<String, Schema> properties = openAPI.getPaths().get("/primitiveBody/inline").getPost().getRequestBody().getContent().get("application/json").getSchema().getProperties();
    assertTrue(properties.get("name") instanceof StringSchema);
}
Also used : DateSchema(io.swagger.v3.oas.models.media.DateSchema) ComposedSchema(io.swagger.v3.oas.models.media.ComposedSchema) DateTimeSchema(io.swagger.v3.oas.models.media.DateTimeSchema) 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) BinarySchema(io.swagger.v3.oas.models.media.BinarySchema) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) StringSchema(io.swagger.v3.oas.models.media.StringSchema) 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 27 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testEmpty.

@Test
public void testEmpty(@Injectable List<AuthorizationValue> auths) {
    String json = "{}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readContents(json, auths, options);
    List<String> messageList = result.getMessages();
    Set<String> messages = new HashSet<>(messageList);
    assertTrue(messages.contains("attribute openapi is missing"));
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 28 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testOptionalParameter.

@Test
public void testOptionalParameter(@Injectable List<AuthorizationValue> auths) {
    String yaml = "openapi: 3.0.1\n" + "paths:\n" + "  \"/pet\":\n" + "    summary: summary\n" + "    description: description\n" + "    post:\n" + "      summary: Add a new pet to the store\n" + "      description: ''\n" + "      operationId: addPet\n" + "      parameters:\n" + "      - name: status\n" + "        in: query\n" + "        description: Status values that need to be considered for filter\n" + "        schema:\n" + "          type: array\n" + "          items:\n" + "            type: string\n" + "            enum:\n" + "            - available\n" + "            - pending\n" + "            - sold\n" + "          default: available";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    SwaggerParseResult result = parser.readContents(yaml, auths, options);
    OpenAPI openAPI = result.getOpenAPI();
    Parameter parameter = openAPI.getPaths().get("/pet").getPost().getParameters().get(0);
    Assert.assertFalse(parameter.getRequired());
}
Also used : ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) 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 29 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class RemoteUrlTest method testAuthorizationHeaderWithMatchingUrl.

@Test
public void testAuthorizationHeaderWithMatchingUrl() throws Exception {
    final String expectedBody = setupStub();
    final String headerName = "Authorization";
    final String headerValue = "foobar";
    final AuthorizationValue authorizationValue = new AuthorizationValue(headerName, headerValue, "header", url -> url.toString().startsWith("http://localhost"));
    final String actualBody = RemoteUrl.urlToString(getUrl(), Arrays.asList(authorizationValue));
    assertEquals(actualBody, expectedBody);
    verify(getRequestedFor(urlEqualTo("/v2/pet/1")).withHeader("Accept", equalTo(EXPECTED_ACCEPTS_HEADER)).withHeader(headerName, equalTo(headerValue)));
}
Also used : AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) Test(org.testng.annotations.Test)

Example 30 with AuthorizationValue

use of io.swagger.v3.parser.core.models.AuthorizationValue in project swagger-parser by swagger-api.

the class ExternalRefProcessorTest method testRelativeRefIncludingUrlRef.

@Test
public void testRelativeRefIncludingUrlRef(@Injectable final Schema mockedModel) throws Exception {
    final RefFormat refFormat = RefFormat.RELATIVE;
    final String url = "https://my.example.remote.url.com/globals.yaml";
    final String expectedResult = "components:\n" + "  schemas:\n" + "    link-object:\n" + "      type: object\n" + "      additionalProperties:\n" + "        \"$ref\": \"#/components/schemas/rel-data\"\n" + "    rel-data:\n" + "      type: object\n" + "      required:\n" + "      - href\n" + "      properties:\n" + "        href:\n" + "          type: string\n" + "        note:\n" + "          type: string\n" + "    result:\n" + "      type: object\n" + "      properties:\n" + "        name:\n" + "          type: string\n" + "        _links:\n" + "          \"$ref\": \"#/components/schemas/link-object\"\n" + "";
    List<AuthorizationValue> auths = null;
    new Expectations() {

        {
            RemoteUrl.urlToString(url, auths);
            times = 1;
            result = expectedResult;
        }
    };
    OpenAPI mockedOpenAPI = new OpenAPI();
    mockedOpenAPI.setComponents(new Components());
    mockedOpenAPI.getComponents().setSchemas(new HashMap<>());
    ResolverCache mockedResolverCache = new ResolverCache(mockedOpenAPI, null, null);
    ExternalRefProcessor processor = new ExternalRefProcessor(mockedResolverCache, mockedOpenAPI);
    processor.processRefToExternalSchema("./relative-with-url/relative-with-url.yaml#/relative-with-url", refFormat);
    assertThat(((Schema) mockedOpenAPI.getComponents().getSchemas().get("relative-with-url").getProperties().get("Foo")).get$ref(), is("https://my.example.remote.url.com/globals.yaml#/components/schemas/link-object"));
    assertThat(mockedOpenAPI.getComponents().getSchemas().keySet().contains("link-object"), is(true));
    assertThat(mockedOpenAPI.getComponents().getSchemas().keySet().contains("rel-data"), is(true));
    // assert that ref is relative ref is resolved. and the file path is from root yaml file.
    assertThat(((Schema) mockedOpenAPI.getComponents().getSchemas().get("relative-with-url").getProperties().get("Bar")).get$ref(), is("./relative-with-url/relative-with-local.yaml#/relative-same-file"));
}
Also used : Expectations(mockit.Expectations) StrictExpectations(mockit.StrictExpectations) Components(io.swagger.v3.oas.models.Components) AuthorizationValue(io.swagger.v3.parser.core.models.AuthorizationValue) RefFormat(io.swagger.v3.parser.models.RefFormat) ResolverCache(io.swagger.v3.parser.ResolverCache) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)44 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)40 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)34 OpenAPI (io.swagger.v3.oas.models.OpenAPI)31 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)21 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)18 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)18 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)17 Schema (io.swagger.v3.oas.models.media.Schema)17 StringSchema (io.swagger.v3.oas.models.media.StringSchema)17 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)16 AuthorizationValue (io.swagger.v3.parser.core.models.AuthorizationValue)13 ByteArraySchema (io.swagger.v3.oas.models.media.ByteArraySchema)8 MapSchema (io.swagger.v3.oas.models.media.MapSchema)8 HashSet (java.util.HashSet)7 BinarySchema (io.swagger.v3.oas.models.media.BinarySchema)5 DateSchema (io.swagger.v3.oas.models.media.DateSchema)5 DateTimeSchema (io.swagger.v3.oas.models.media.DateTimeSchema)5 ArrayList (java.util.ArrayList)5 Expectations (mockit.Expectations)5