Java Code Examples for io.swagger.v3.oas.models.OpenAPI#setComponents()

The following examples show how to use io.swagger.v3.oas.models.OpenAPI#setComponents() . 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: JavaInheritanceTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test(description = "convert a composed model with parent")
public void javaInheritanceTest() {
    final Schema parentModel = new Schema().name("Base");

    final Schema schema = new ComposedSchema()
            .addAllOfItem(new Schema().$ref("Base"))
            .name("composed");

    OpenAPI openAPI = TestUtils.createOpenAPI();
    openAPI.setComponents(new Components()
            .addSchemas(parentModel.getName(),parentModel)
            .addSchemas(schema.getName(), schema)
    );

    final JavaClientCodegen codegen = new JavaClientCodegen();
    codegen.setOpenAPI(openAPI);
    final CodegenModel cm = codegen.fromModel("sample", schema);

    Assert.assertEquals(cm.name, "sample");
    Assert.assertEquals(cm.classname, "Sample");
    Assert.assertEquals(cm.parent, "Base");
    Assert.assertEquals(cm.imports, Sets.newHashSet("Base"));
}
 
Example 2
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasicInput() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema user = new Schema();
    user.addProperties("name", new StringSchema());

    openAPI.path("/foo/baz", new PathItem()
            .post(new Operation()
                    .requestBody(new RequestBody()
                            .content(new Content().addMediaType("*/*",new MediaType().schema(new Schema().$ref("User")))))));

    openAPI.getComponents().addSchemas("User", user);

    new InlineModelResolver().flatten(openAPI);
}
 
Example 3
Source File: DefaultCodegenTest.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
@Test
public void testHasBodyParameter() {
    final Schema refSchema = new Schema<>().$ref("#/components/schemas/Pet");
    Operation pingOperation = new Operation()
            .responses(
                    new ApiResponses().addApiResponse("204", new ApiResponse()
                            .description("Ok response")));
    Operation createOperation = new Operation()
            .requestBody(new RequestBody()
                    .content(new Content().addMediaType("application/json",
                            new MediaType().schema(refSchema))))
            .responses(
                    new ApiResponses().addApiResponse("201", new ApiResponse()
                            .description("Created response")));
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
    openAPI.getComponents().addSchemas("Pet", new ObjectSchema());

    final DefaultCodegen codegen = new DefaultCodegen();

    Assert.assertEquals(codegen.hasBodyParameter(openAPI, pingOperation), false);
    Assert.assertEquals(codegen.hasBodyParameter(openAPI, createOperation), true);
}
 
Example 4
Source File: TestUtils.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
public static OpenAPI createOpenAPI() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
    openAPI.setPaths(new Paths());

    final Info info = new Info();
    info.setDescription("API under test");
    info.setVersion("1.0.7");
    info.setTitle("My title");
    openAPI.setInfo(info);

    final Server server = new Server();
    server.setUrl("https://localhost:9999/root");
    openAPI.setServers(Collections.singletonList(server));
    return openAPI;
}
 
Example 5
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testArbitraryObjectModelWithArrayInlineWithoutTitle() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema items = new ObjectSchema();
    items.setDefault("default");
    items.setReadOnly(false);
    items.setDescription("description");
    items.setName("name");
    items.addProperties("arbitrary", new ObjectSchema());

    openAPI.getComponents().addSchemas("User", new ArraySchema().items(items).addRequiredItem("name"));

    new InlineModelResolver().flatten(openAPI);

    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);
    ArraySchema am = (ArraySchema) model;
    Schema inner = am.getItems();
    assertTrue(inner.get$ref() != null);

    Schema userInner = openAPI.getComponents().getSchemas().get("User_inner");
    assertNotNull(userInner);
    Schema inlineProp = (Schema)userInner.getProperties().get("arbitrary");
    assertTrue(inlineProp instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) inlineProp;
    assertNull(op.getProperties());
}
 
Example 6
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineModelTestWithTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());


    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("UserAddressTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    Schema schema =  new Schema();
    schema.setName("user");
    schema.setDescription("a common user");
    List<String> required = new ArrayList<>();
    required.add("address");
    schema.setRequired(required);
    schema.addProperties("name", new StringSchema());
    schema.addProperties("address", objectSchema);


    openAPI.getComponents().addSchemas("User", schema);

    new InlineModelResolver().flatten(openAPI);

    Schema user = openAPI.getComponents().getSchemas().get("User");

    assertNotNull(user);
    Schema address = (Schema)user.getProperties().get("address");
    assertTrue( address.get$ref() != null);

    Schema userAddressTitle = openAPI.getComponents().getSchemas().get("UserAddressTitle");
    assertNotNull(userAddressTitle);
    assertNotNull(userAddressTitle.getProperties().get("city"));
    assertNotNull(userAddressTitle.getProperties().get("street"));
}
 
Example 7
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineModelTestWithoutTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());


    Schema objectSchema = new ObjectSchema();
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    Schema schema =  new Schema();
    schema.setName("user");
    schema.setDescription("a common user");
    List<String> required = new ArrayList<>();
    required.add("address");
    schema.setRequired(required);
    schema.addProperties("name", new StringSchema());
    schema.addProperties("address", objectSchema);


    openAPI.getComponents().addSchemas("User", schema);

    new InlineModelResolver().flatten(openAPI);

    Schema user = openAPI.getComponents().getSchemas().get("User");

    assertNotNull(user);
    Schema address = (Schema)user.getProperties().get("address");
    assertTrue((address.get$ref()!= null));
    Schema userAddress = openAPI.getComponents().getSchemas().get("User_address");
    assertNotNull(userAddress);
    assertNotNull(userAddress.getProperties().get("city"));
    assertNotNull(userAddress.getProperties().get("street"));
}
 
Example 8
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testArbitraryObjectModelInline() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema userSchema = new Schema();
    userSchema.setName("user");
    userSchema.setDescription("a common user");
    userSchema.addProperties("name", new StringSchema());

    ObjectSchema objectSchema = new ObjectSchema();
    objectSchema.setTitle("title");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");

    userSchema.addProperties("arbitrary", objectSchema);
    List required = new ArrayList();
    required.add("arbitrary");
    userSchema.setRequired(required);


    openAPI.getComponents().addSchemas("User", userSchema);

    new InlineModelResolver().flatten(openAPI);

    Schema user = openAPI.getComponents().getSchemas().get("User");
    assertNotNull(user);
    Schema inlineProp = (Schema) user.getProperties().get("arbitrary");
    assertTrue(inlineProp instanceof ObjectSchema);
    assertNull(inlineProp.getProperties());
}
 
Example 9
Source File: InlineModelResolverTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineModelTestWithTitleWithSpaces() {
    OpenAPI openapi = new OpenAPI();
    openapi.setComponents(new Components());
    openapi.getComponents().addSchemas("User", new ObjectSchema()
            .name("user")
            .description("a common user")
            .addProperties("name", new StringSchema())
            .addProperties("address", new ObjectSchema()
                    .title("User Address Title")
                    .readOnly(false)
                    .description("description")
                    .name("name")
                    .addProperties("street", new StringSchema())
                    .addProperties("city", new StringSchema())));

    new InlineModelResolver().flatten(openapi);

    Schema user = openapi.getComponents().getSchemas().get("User");

    assertNotNull(user);
    assertTrue(user.getProperties().get("address") instanceof Schema);

    Schema address = openapi.getComponents().getSchemas().get("User_Address_Title");
    assertNotNull(address);
    assertNotNull(address.getProperties().get("city"));
    assertNotNull(address.getProperties().get("street"));
}
 
Example 10
Source File: InlineModelResolverTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Test
public void resolveInlineModelTestWithTitle() {
    OpenAPI openapi = new OpenAPI();
    openapi.setComponents(new Components());
    openapi.getComponents().addSchemas("User", new ObjectSchema()
            .name("user")
            .description("a common user")
            .addProperties("name", new StringSchema())
            .addProperties("address", new ObjectSchema()
                    .title("UserAddressTitle")
                    .readOnly(false)
                    .description("description")
                    .name("name")
                    .addProperties("street", new StringSchema())
                    .addProperties("city", new StringSchema())));

    new InlineModelResolver().flatten(openapi);

    Schema user = openapi.getComponents().getSchemas().get("User");

    assertNotNull(user);
    assertTrue(user.getProperties().get("address") instanceof Schema);

    Schema address = openapi.getComponents().getSchemas().get("UserAddressTitle");
    assertNotNull(address);
    assertNotNull(address.getProperties().get("city"));
    assertNotNull(address.getProperties().get("street"));
}
 
Example 11
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 5 votes vote down vote up
@Test
public void testArbitraryObjectModelWithArrayInlineWithTitle() {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema items = new ObjectSchema();
    items.setTitle("InnerUserTitle");
    items.setDefault("default");
    items.setReadOnly(false);
    items.setDescription("description");
    items.setName("name");
    items.addProperties("arbitrary", new ObjectSchema());

    openAPI.getComponents().addSchemas("User", new ArraySchema().items(items).addRequiredItem("name"));

    new InlineModelResolver().flatten(openAPI);

    Schema model = openAPI.getComponents().getSchemas().get("User");
    assertTrue(model instanceof ArraySchema);
    ArraySchema am = (ArraySchema) model;
    Schema inner = am.getItems();
    assertTrue(inner.get$ref() != null);

    Schema userInner = openAPI.getComponents().getSchemas().get("InnerUserTitle");
    assertNotNull(userInner);
    Schema inlineProp = (Schema) userInner.getProperties().get("arbitrary");
    assertTrue(inlineProp instanceof ObjectSchema);
    ObjectSchema op = (ObjectSchema) inlineProp;
    assertNull(op.getProperties());
}
 
Example 12
Source File: JavaModelTest.java    From openapi-generator with Apache License 2.0 5 votes vote down vote up
@Test(description = "convert referenced string property in an object")
public void stringPropertyReferencedInObjectTest() {
    final Schema property = new StringSchema().maxLength(10).minLength(3).pattern("^[A-Z]+$");
    final Schema myObject = new ObjectSchema().addProperties("somePropertyWithMinMaxAndPattern", new ObjectSchema().$ref("refObj"));

    final DefaultCodegen codegen = new JavaClientCodegen();
    OpenAPI openAPI = TestUtils.createOpenAPI();
    openAPI.setComponents(new Components()
            .addSchemas("myObject", myObject)
            .addSchemas("refObj", property)
    );
    codegen.setOpenAPI(openAPI);
    CodegenModel cm = codegen.fromModel("myObject", myObject);

    Assert.assertEquals(cm.getVars().size(), 1);
    CodegenProperty cp = cm.getVars().get(0);
    Assert.assertEquals(cp.baseName, "somePropertyWithMinMaxAndPattern");
    Assert.assertEquals(cp.nameInCamelCase, "SomePropertyWithMinMaxAndPattern");
    Assert.assertEquals(cp.nameInSnakeCase, "SOME_PROPERTY_WITH_MIN_MAX_AND_PATTERN");
    Assert.assertEquals(cp.dataType, "String");
    Assert.assertEquals(cp.name, "somePropertyWithMinMaxAndPattern");
    Assert.assertEquals(cp.baseType, "String");
    Assert.assertFalse(cp.isContainer);
    Assert.assertFalse(cp.isLong);
    Assert.assertFalse(cp.isInteger);
    Assert.assertTrue(cp.isString);
    Assert.assertEquals(cp.getter, "getSomePropertyWithMinMaxAndPattern");
    Assert.assertEquals(cp.minLength, Integer.valueOf(3));
    Assert.assertEquals(cp.maxLength, Integer.valueOf(10));
    Assert.assertEquals(cp.pattern, "^[A-Z]+$");
}
 
Example 13
Source File: OpenAPIGenerator.java    From spring-openapi with MIT License 5 votes vote down vote up
public OpenAPI generate(OpenApiGeneratorConfig openApiGeneratorConfig) {
      logger.info("Starting OpenAPI generation");
environment = openApiGeneratorConfig.getEnvironment();
      initializeExampleInterceptor(openApiGeneratorConfig);
      OpenAPI openAPI = new OpenAPI();
      openAPI.setComponents(createComponentsWrapper());
      openAPI.setPaths(createPathsWrapper());
      openAPI.setInfo(info);
      logger.info("OpenAPI generation done!");
      return openAPI;
  }
 
Example 14
Source File: InlineModelResolverTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Test
public void resolveInlineModel2DifferentInnerModelsWIthSameTitle() {
    OpenAPI openapi = new OpenAPI();
    openapi.setComponents(new Components());
    openapi.getComponents().addSchemas("User", new ObjectSchema()
            .name("user")
            .description("a common user")
            .addProperties("name", new StringSchema())
            .addProperties("address", new ObjectSchema()
                    .title("UserAddressTitle")
                    .readOnly(false)
                    .description("description")
                    .name("name")
                    .addProperties("street", new StringSchema())
                    .addProperties("city", new StringSchema())));

    openapi.getComponents().addSchemas("AnotherUser", new ObjectSchema()
            .name("AnotherUser")
            .description("a common user")
            .addProperties("name", new StringSchema())
            .addProperties("lastName", new StringSchema())
            .addProperties("address", new ObjectSchema()
                    .title("UserAddressTitle")
                    .readOnly(false)
                    .description("description")
                    .name("name")
                    .addProperties("street", new StringSchema())
                    .addProperties("city", new StringSchema())
                    .addProperties("apartment", new StringSchema())));

    new InlineModelResolver().flatten(openapi);

    Schema user = openapi.getComponents().getSchemas().get("User");

    assertNotNull(user);
    assertTrue(user.getProperties().get("address") instanceof Schema);

    Schema address = openapi.getComponents().getSchemas().get("UserAddressTitle");
    assertNotNull(address);
    assertNotNull(address.getProperties().get("city"));
    assertNotNull(address.getProperties().get("street"));
    Schema duplicateAddress = openapi.getComponents().getSchemas().get("UserAddressTitle_1");
    assertNotNull(duplicateAddress);
    assertNotNull(duplicateAddress.getProperties().get("city"));
    assertNotNull(duplicateAddress.getProperties().get("street"));
    assertNotNull(duplicateAddress.getProperties().get("apartment"));
}
 
Example 15
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void resolveInlineModel2EqualInnerModels() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());


    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("UserAddressTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    Schema schema =  new Schema();
    schema.setName("user");
    schema.setDescription("a common user");
    List<String> required = new ArrayList<>();
    required.add("address");
    schema.setRequired(required);
    schema.addProperties("name", new StringSchema());
    schema.addProperties("address", objectSchema);


    openAPI.getComponents().addSchemas("User", schema);

    Schema addressSchema = new ObjectSchema();
    addressSchema.setTitle("UserAddressTitle");
    addressSchema.setDefault("default");
    addressSchema.setReadOnly(false);
    addressSchema.setDescription("description");
    addressSchema.setName("name");
    addressSchema.addProperties("street", new StringSchema());
    addressSchema.addProperties("city", new StringSchema());

    Schema anotherSchema =  new Schema();
    anotherSchema.setName("user");
    anotherSchema.setDescription("a common user");
    List<String> requiredFields = new ArrayList<>();
    requiredFields.add("address");
    anotherSchema.setRequired(requiredFields);
    anotherSchema.addProperties("name", new StringSchema());
    anotherSchema.addProperties("lastName", new StringSchema());
    anotherSchema.addProperties("address", addressSchema);

    openAPI.getComponents().addSchemas("AnotherUser", anotherSchema);

    new InlineModelResolver().flatten(openAPI);

    Schema user = openAPI.getComponents().getSchemas().get("User");

    assertNotNull(user);
    Schema addressSchema1 = (Schema) user.getProperties().get("address");
    assertTrue(addressSchema1.get$ref() != null);

    Schema address = openAPI.getComponents().getSchemas().get("UserAddressTitle");
    assertNotNull(address);
    assertNotNull(address.getProperties().get("city"));
    assertNotNull(address.getProperties().get("street"));
    Schema duplicateAddress = openAPI.getComponents().getSchemas().get("UserAddressTitle_0");
    assertNull(duplicateAddress);
}
 
Example 16
Source File: TestUtils.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
public static OpenAPI createOpenAPIWithOneSchema(String name, Schema schema) {
    OpenAPI openAPI = createOpenAPI();
    openAPI.setComponents(new Components());
    openAPI.getComponents().addSchemas(name, schema);
    return openAPI;
}
 
Example 17
Source File: ProtoOpenAPI.java    From product-microgateway with Apache License 2.0 4 votes vote down vote up
ProtoOpenAPI() {
    openAPI = new OpenAPI();
    openAPI.setComponents(new Components());
}
 
Example 18
Source File: InlineModelResolverTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@Test
public void resolveInlineModel2DifferentInnerModelsWIthSameTitle() throws Exception {
    OpenAPI openAPI = new OpenAPI();
    openAPI.setComponents(new Components());

    Schema objectSchema = new ObjectSchema();
    objectSchema.setTitle("UserAddressTitle");
    objectSchema.setDefault("default");
    objectSchema.setReadOnly(false);
    objectSchema.setDescription("description");
    objectSchema.setName("name");
    objectSchema.addProperties("street", new StringSchema());
    objectSchema.addProperties("city", new StringSchema());

    Schema schema =  new Schema();
    schema.setName("user");
    schema.setDescription("a common user");
    List<String> required = new ArrayList<>();
    required.add("address");
    schema.setRequired(required);
    schema.addProperties("name", new StringSchema());
    schema.addProperties("address", objectSchema);


    openAPI.getComponents().addSchemas("User", schema);

    Schema addressSchema = new ObjectSchema();
    addressSchema.setTitle("UserAddressTitle");
    addressSchema.setDefault("default");
    addressSchema.setReadOnly(false);
    addressSchema.setDescription("description");
    addressSchema.setName("name");
    addressSchema.addProperties("street", new StringSchema());
    addressSchema.addProperties("city", new StringSchema());
    addressSchema.addProperties("apartment", new StringSchema());


    Schema anotherSchema =  new Schema();
    anotherSchema.setName("AnotherUser");
    anotherSchema.setDescription("a common user");
    List<String> requiredFields = new ArrayList<>();
    requiredFields.add("address");
    anotherSchema.setRequired(requiredFields);
    anotherSchema.addProperties("name", new StringSchema());
    anotherSchema.addProperties("lastName", new StringSchema());
    anotherSchema.addProperties("address", addressSchema);


    openAPI.getComponents().addSchemas("AnotherUser", anotherSchema);

    new InlineModelResolver().flatten(openAPI);

    Schema user = openAPI.getComponents().getSchemas().get("User");

    assertNotNull(user);
    Schema userAddress = (Schema) user.getProperties().get("address");
    assertTrue( userAddress.get$ref()!= null);

    Schema address = openAPI.getComponents().getSchemas().get("UserAddressTitle");
    assertNotNull(address);
    assertNotNull(address.getProperties().get("city"));
    assertNotNull(address.getProperties().get("street"));
    Schema duplicateAddress = openAPI.getComponents().getSchemas().get("UserAddressTitle_1");
    assertNotNull(duplicateAddress);
    assertNotNull(duplicateAddress.getProperties().get("city"));
    assertNotNull(duplicateAddress.getProperties().get("street"));
    assertNotNull(duplicateAddress.getProperties().get("apartment"));
}
 
Example 19
Source File: OpenAPIDeserializer.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
public OpenAPI parseRoot(JsonNode node, ParseResult result, String path) {
    String location = "";
    OpenAPI openAPI = new OpenAPI();
    if (node.getNodeType().equals(JsonNodeType.OBJECT)) {
        ObjectNode rootNode = (ObjectNode) node;

        // required
        String value = getString("openapi", rootNode, true, location, result);

        // we don't even try if the version isn't there
        if(value == null || !value.startsWith("3.0")) {
            return null;
        }
        openAPI.setOpenapi(value);

        ObjectNode obj = getObject("info", rootNode, true, location, result);
        if (obj != null) {
            Info info = getInfo(obj, "info", result);
            openAPI.setInfo(info);
        }

        obj = getObject("components", rootNode, false, location, result);
        if (obj != null) {
            Components components = getComponents(obj, "components", result);
            openAPI.setComponents(components);
            this.components=components;
        }

        obj = getObject("paths", rootNode, true, location, result);
        if (obj != null) {
            Paths paths = getPaths(obj, "paths", result);
            openAPI.setPaths(paths);
        }


        ArrayNode array = getArray("servers", rootNode, false, location, result);
        if (array != null && array.size() > 0) {
            openAPI.setServers(getServersList(array, String.format("%s.%s", location, "servers"), result, path));
        }else {
            Server defaultServer = new Server();
            defaultServer.setUrl("/");
            List<Server>  servers = new ArrayList<>();
            servers.add(defaultServer);
            openAPI.setServers(servers);
        }

        obj = getObject("externalDocs", rootNode, false, location, result);
        if (obj != null) {
            ExternalDocumentation externalDocs = getExternalDocs(obj, "externalDocs", result);
            openAPI.setExternalDocs(externalDocs);
        }

        array = getArray("tags", rootNode, false, location, result);
        if (array != null && array.size() > 0) {
            openAPI.setTags(getTagList(array, "tags", result));
        }

        array = getArray("security", rootNode, false, location, result);
        if (array != null && array.size() > 0) {
            List<SecurityRequirement> securityRequirements = getSecurityRequirementsList(array, "security", result);
            if (securityRequirements != null && securityRequirements. size() > 0) {
                openAPI.setSecurity(securityRequirements);
            }
        }

        Map <String,Object> extensions = getExtensions(rootNode);
        if(extensions != null && extensions.size() > 0) {
            openAPI.setExtensions(extensions);
        }

        Set<String> keys = getKeys(rootNode);
        for(String key : keys) {
            if(!ROOT_KEYS.contains(key) && !key.startsWith("x-")) {
                result.extra(location, key, node.get(key));
            }
        }

    } else {
        result.invalidType(location, "openapi", "object", node);
        result.invalid();
        return null;
    }

    return openAPI;
}
 
Example 20
Source File: ExternalRefProcessorTest.java    From swagger-parser with Apache License 2.0 4 votes vote down vote up
@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")
   );
}