Search in sources :

Example 31 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class InlineModelResolverTest method testArbitraryObjectBodyParamWithArray.

@Test
public void testArbitraryObjectBodyParamWithArray() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.path("/hello", new PathItem().get(new Operation().requestBody(new RequestBody().content(new Content().addMediaType("*/*", new MediaType().schema(new ArraySchema().items(new ObjectSchema())))))));
    new InlineModelResolver().flatten(openAPI);
    RequestBody requestBody = openAPI.getPaths().get("/hello").getGet().getRequestBody();
    Schema schema = requestBody.getContent().get("*/*").getSchema();
    assertTrue(schema instanceof ArraySchema);
    ArraySchema arraySchema = (ArraySchema) schema;
    Schema inner = arraySchema.getItems();
    assertTrue(inner instanceof ObjectSchema);
    ObjectSchema property = (ObjectSchema) inner;
    assertNotNull(property);
    assertNull(property.getProperties());
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) ArraySchema(io.swagger.v3.oas.models.media.ArraySchema) 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) 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 32 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class InlineModelResolverTest method resolveInlineRequestBody_stripsDotsFromPath.

@Test
public void resolveInlineRequestBody_stripsDotsFromPath() 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("/api/Cloud.Greet.Hello", pathItem);
    new InlineModelResolver(true, true).flatten(openAPI);
    Operation getOperation = openAPI.getPaths().get("/api/Cloud.Greet.Hello").getGet();
    RequestBody body = getOperation.getRequestBody();
    assertEquals("use dot as common word separator: as it occurs frequently on OData services", "#/components/schemas/ApiCloudGreetHelloBody", body.getContent().get("*/*").getSchema().get$ref());
    Schema bodySchema = openAPI.getComponents().getSchemas().get("ApiCloudGreetHelloBody");
    assertTrue(bodySchema instanceof Schema);
    assertNotNull(bodySchema.getProperties().get("address"));
}
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) MediaType(io.swagger.v3.oas.models.media.MediaType) StringSchema(io.swagger.v3.oas.models.media.StringSchema) 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 33 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-parser by swagger-api.

the class OpenAPIV3ParserTest method testRemotePathItemIssue1103.

@Test
public void testRemotePathItemIssue1103(@Injectable final List<AuthorizationValue> auths) throws Exception {
    OpenAPI result = new OpenAPIV3Parser().read("issue-1103/remote-pathItem-swagger.yaml");
    Assert.assertNotNull(result);
    Assert.assertNotNull(result.getPaths().get("/Translation/{lang}"));
    Assert.assertEquals(result.getPaths().get("/Translation/{lang}").getPut().getParameters().get(0).getName(), "lang");
}
Also used : OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Example 34 with PathItem

use of io.swagger.v3.oas.models.PathItem 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 35 with PathItem

use of io.swagger.v3.oas.models.PathItem in project swagger-core by swagger-api.

the class JsonDeserializationTest method testDeserializeAPathRef.

@Test
public void testDeserializeAPathRef() throws Exception {
    final OpenAPI oas = TestUtils.deserializeJsonFileFromClasspath("specFiles/pathRef.json", OpenAPI.class);
    final PathItem petPath = oas.getPaths().get("/pet");
    assertNotNull(petPath.get$ref());
    assertEquals(petPath.get$ref(), "http://my.company.com/paths/health.json");
    assertTrue(oas.getPaths().get("/user") instanceof PathItem);
}
Also used : PathItem(io.swagger.v3.oas.models.PathItem) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Test(org.testng.annotations.Test)

Aggregations

PathItem (io.swagger.v3.oas.models.PathItem)135 OpenAPI (io.swagger.v3.oas.models.OpenAPI)109 Operation (io.swagger.v3.oas.models.Operation)104 Test (org.testng.annotations.Test)92 Schema (io.swagger.v3.oas.models.media.Schema)57 ArraySchema (io.swagger.v3.oas.models.media.ArraySchema)50 IntegerSchema (io.swagger.v3.oas.models.media.IntegerSchema)45 StringSchema (io.swagger.v3.oas.models.media.StringSchema)44 ObjectSchema (io.swagger.v3.oas.models.media.ObjectSchema)42 ApiResponse (io.swagger.v3.oas.models.responses.ApiResponse)42 MediaType (io.swagger.v3.oas.models.media.MediaType)41 Content (io.swagger.v3.oas.models.media.Content)38 Paths (io.swagger.v3.oas.models.Paths)36 ApiResponses (io.swagger.v3.oas.models.responses.ApiResponses)35 Parameter (io.swagger.v3.oas.models.parameters.Parameter)29 Components (io.swagger.v3.oas.models.Components)26 SwaggerParseResult (io.swagger.v3.parser.core.models.SwaggerParseResult)25 RequestBody (io.swagger.v3.oas.models.parameters.RequestBody)24 Map (java.util.Map)22 HashMap (java.util.HashMap)20