Search in sources :

Example 6 with In

use of io.swagger.v3.oas.models.security.SecurityScheme.In in project swagger-parser by swagger-api.

the class NetworkReferenceTest method testValidateExternalRefsTrueRemote.

@Test(description = "option true, adds Original Location to messages when ref is remote")
public void testValidateExternalRefsTrueRemote() throws Exception {
    ParseOptions options = new ParseOptions();
    options.setValidateExternalRefs(true);
    options.setResolve(true);
    new Expectations() {

        {
            remoteUrl.urlToString("http://localhost:8080/swos-443/root.yaml", new ArrayList<>());
            result = issue_443_yaml;
            remoteUrl.urlToString("http://localhost:8080/swos-443/ref.yaml", new ArrayList<>());
            result = issue_443_ref_yaml;
        }
    };
    SwaggerParseResult result = new OpenAPIV3Parser().readLocation("http://localhost:8080/swos-443/root.yaml", null, options);
    OpenAPI openAPI = result.getOpenAPI();
    assertNotNull(result.getMessages());
    assertEquals(result.getMessages().size(), 19);
    assertNotNull(openAPI);
    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"));
}
Also used : Expectations(mockit.Expectations) 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 7 with In

use of io.swagger.v3.oas.models.security.SecurityScheme.In in project swagger-parser by swagger-api.

the class SchemaProcessor method processReferenceSchema.

private void processReferenceSchema(Schema schema) {
    /* if this is a URL or relative ref:
        1) we need to load it into memory.
        2) shove it into the #/components/schemas
        3) update the RefModel to point to its location in #/components/schemas
     */
    RefFormat refFormat = computeRefFormat(schema.get$ref());
    String $ref = schema.get$ref();
    if (isAnExternalRefFormat(refFormat)) {
        final String newRef = externalRefProcessor.processRefToExternalSchema($ref, refFormat);
        if (newRef != null) {
            schema.set$ref(RefType.SCHEMAS.getInternalPrefix() + newRef);
        }
    }
}
Also used : RefUtils.isAnExternalRefFormat(io.swagger.v3.parser.util.RefUtils.isAnExternalRefFormat) RefUtils.computeRefFormat(io.swagger.v3.parser.util.RefUtils.computeRefFormat) RefFormat(io.swagger.v3.parser.models.RefFormat)

Example 8 with In

use of io.swagger.v3.oas.models.security.SecurityScheme.In in project swagger-parser by swagger-api.

the class OpenAPIResolverTest method testParameterOnPathLevel.

@Test
public void testParameterOnPathLevel() throws Exception {
    String yaml = "openapi: 3.0.1\n" + "info:\n" + "  version: '1.0.0'\n" + "  title: 'title'\n" + "  description: 'description'\n" + "paths:\n" + "  /foo:\n" + "    parameters:\n" + "      - in: query\n" + "        name: bar\n" + "        schema:\n" + "          type: string\n" + "    get:\n" + "      responses:\n" + "        '200':\n" + "          description: OK";
    ParseOptions options = new ParseOptions();
    options.setResolve(true);
    OpenAPI openAPI = new OpenAPIV3Parser().readContents(yaml, null, options).getOpenAPI();
    assertNotNull(openAPI);
    List<Parameter> getParameters = openAPI.getPaths().get("/foo").getGet().getParameters();
    assertNotNull(getParameters);
    assertEquals(1, getParameters.size());
    assertEquals("bar", getParameters.get(0).getName());
}
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) PathParameter(io.swagger.v3.oas.models.parameters.PathParameter) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 9 with In

use of io.swagger.v3.oas.models.security.SecurityScheme.In in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testSkipInlineMatchesFalse.

@Test
public void testSkipInlineMatchesFalse() {
    final OpenAPI openAPI = new OpenAPI();
    final InlineModelResolver inlineModelResolver = new InlineModelResolver();
    final Schema operationAlphaInAsset = new ObjectSchema();
    operationAlphaInAsset.setTitle("operationAlphaInAsset");
    operationAlphaInAsset.addProperties("id1", new IntegerSchema());
    operationAlphaInAsset.addProperties("id2", new IntegerSchema());
    final Schema operationAlphaIn = new ObjectSchema();
    operationAlphaIn.setTitle("operationAlphaIn");
    operationAlphaIn.addProperties("asset", operationAlphaInAsset);
    final Schema operationAlphaRequest = new ObjectSchema();
    operationAlphaRequest.setTitle("operationAlphaRequest");
    operationAlphaRequest.addProperties("in", operationAlphaIn);
    final Schema operationBetaInAsset = new ObjectSchema();
    operationBetaInAsset.setTitle("operationBetaInAsset");
    operationBetaInAsset.addProperties("id1", new IntegerSchema());
    operationBetaInAsset.addProperties("id2", new IntegerSchema());
    final Schema operationBetaIn = new ObjectSchema();
    operationBetaIn.setTitle("operationBetaIn");
    operationBetaIn.addProperties("asset", operationBetaInAsset);
    final Schema operationBetaRequest = new ObjectSchema();
    operationBetaRequest.setTitle("operationBetaRequest");
    operationBetaRequest.addProperties("in", operationBetaIn);
    openAPI.path("/operationAlpha", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(operationAlphaRequest))))));
    openAPI.path("/operationBeta", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(operationBetaRequest))))));
    inlineModelResolver.flatten(openAPI);
    assertNotNull(openAPI);
    assertNotNull(openAPI.getComponents());
    assertNotNull(openAPI.getComponents().getSchemas());
    assertEquals(openAPI.getComponents().getSchemas().size(), 6);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) Content(io.swagger.v3.oas.models.media.Content) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) Schema(io.swagger.v3.oas.models.media.Schema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) StringSchema(io.swagger.v3.oas.models.media.StringSchema) ObjectSchema(io.swagger.v3.oas.models.media.ObjectSchema) IntegerSchema(io.swagger.v3.oas.models.media.IntegerSchema) MediaType(io.swagger.v3.oas.models.media.MediaType) Operation(io.swagger.v3.oas.models.Operation) OpenAPI(io.swagger.v3.oas.models.OpenAPI) RequestBody(io.swagger.v3.oas.models.parameters.RequestBody) Test(org.testng.annotations.Test)

Example 10 with In

use of io.swagger.v3.oas.models.security.SecurityScheme.In in project swagger-parser by swagger-api.

the class OpenAPIDeserializerTest method testParamContent.

@Test
public void testParamContent() {
    String json = "{" + "  \"openapi\": \"3.0.0\"," + "  \"info\": {" + "    \"title\": \"Operations\"," + "    \"version\": \"0.0.0\"" + "  }," + "  \"paths\": {" + "    \"/operations\": {" + "      \"post\": {" + "        \"parameters\": [" + "          {" + "            \"name\": \"param0\"," + "            \"in\": \"query\"," + "            \"content\": {" + "            }" + "          }," + "          {" + "            \"name\": \"param1\"," + "            \"in\": \"query\"," + "            \"content\": {" + "              \"text/plain\": {" + "              }" + "            }" + "          }," + "          {" + "            \"name\": \"param2\"," + "            \"in\": \"query\"," + "            \"content\": {" + "              \"text/plain\": {" + "              }," + "              \"application/json\": {" + "                \"schema\": {" + "                  \"type\": \"object\"" + "                }" + "              }" + "            }" + "          }" + "        ]," + "        \"responses\": {" + "          \"default\": {" + "            \"description\": \"None\"" + "          }" + "        }" + "      }" + "    }" + "  }" + "}";
    OpenAPIV3Parser parser = new OpenAPIV3Parser();
    SwaggerParseResult result = parser.readContents(json, null, null);
    Operation post = result.getOpenAPI().getPaths().get("/operations").getPost();
    Parameter param0 = post.getParameters().stream().filter(p -> "param0".equals(p.getName())).findFirst().orElseThrow(() -> new IllegalStateException("Can't find parameter=param0"));
    assertEquals(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param0].content with no media type is unsupported"), true, "No media types error reported");
    assertEquals(param0.getContent(), null, "Empty content");
    Parameter param1 = post.getParameters().stream().filter(p -> "param1".equals(p.getName())).findFirst().orElseThrow(() -> new IllegalStateException("Can't find parameter=param1"));
    assertEquals(param1.getContent().size(), 1, "Valid content size");
    Parameter param2 = post.getParameters().stream().filter(p -> "param2".equals(p.getName())).findFirst().orElseThrow(() -> new IllegalStateException("Can't find parameter=param2"));
    assertEquals(result.getMessages().contains("attribute paths.'/operations'(post).parameters.[param2].content with multiple media types is unsupported"), true, "Multiple media types error reported");
    assertEquals(param2.getContent(), null, "Content with multiple media types");
    assertEquals(result.getMessages().size(), 2, "Messages");
}
Also used : Parameter(io.swagger.v3.oas.models.parameters.Parameter) QueryParameter(io.swagger.v3.oas.models.parameters.QueryParameter) SwaggerParseResult(io.swagger.v3.parser.core.models.SwaggerParseResult) Operation(io.swagger.v3.oas.models.Operation) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) Test(org.testng.annotations.Test)

Aggregations

Test (org.testng.annotations.Test)130 OpenAPI (io.swagger.v3.oas.models.OpenAPI)108 Parameter (io.swagger.v3.oas.models.parameters.Parameter)51 Schema (io.swagger.v3.oas.models.media.Schema)49 StringSchema (io.swagger.v3.oas.models.media.StringSchema)44 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)40 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)39 QueryParameter (io.swagger.v3.oas.models.parameters.QueryParameter)39 Operation (io.swagger.v3.oas.annotations.Operation)36 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)36 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)31 Operation (io.swagger.v3.oas.models.Operation)28 PathItem (io.swagger.v3.oas.models.PathItem)27 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)27 ComposedSchema (io.swagger.v3.oas.models.media.ComposedSchema)25 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)25 Map (java.util.Map)25 HashMap (java.util.HashMap)23 PathParameter (io.swagger.v3.oas.models.parameters.PathParameter)22 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)21