ZenWave360/json-schema-ref-parser-jvm

File not closed

Closed this issue · 4 comments

First, thanks a lot for this lib! Using the $RefParser(file) and $RefParser(uri) constructors, source files remains locked, in runtime file system don t let you delete used files. I used the $RefParser(json, uri) and it works.

I think that the issue can be solved by close the FileInputStreams (will be nice the ByteArrayInputStream too;) in the Parser class, methods parse.

The FileResolver works because the Files.readAllBytes closes the file automatically.

hi @waldimiro
would you mind creating a PR for this?
it would speedup the release of fix

Hi @ivangsa

hier my Test class...

import com.swissimpact.commons.SystemException;
import io.zenwave360.jsonrefparser.$RefParser;
import io.zenwave360.jsonrefparser.$Refs;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Map;

/**
 * Tests
 */
public class RefParserTests {


	/** LOGGER */
	private static final Logger LOGGER = LoggerFactory.getLogger(RefParserTests.class.getName());

	@Test
	public void test() throws Exception {

		Files.copy(Path.of("src/test/resources/json/schema/Identifiable.schema.json"),
				Path.of("src/test/resources/json/schema/IdentifiableCopy.schema.json"),
				StandardCopyOption.REPLACE_EXISTING);

		// Get JSON Schema parser and validator
		Map<String, Object> parsed = this.parse(Path.of("src/test/resources/json/schema/IdentifiableCopy.schema.json").toUri());

		LOGGER.info(parsed.toString());

		Files.delete(Path.of("src/test/resources/json/schema/IdentifiableCopy.schema.json"));

	}

	private Map<String, Object> parse(URI uri) {

		try {

			// 1. Get parser with JSON and URI doesn't work
			//$RefParser parser = new $RefParser(Files.readString(Path.of(uri)), uri);

			// 2. Get parser with URI doesn't work
			$RefParser parser = new $RefParser(uri);

			// Merge $refs in one file
			$Refs refs = parser.parse().dereference().getRefs();

			return refs.schema();
		}
		catch(Exception e) {
			throw new SystemException(e.getMessage(), e);
		}
	}
}

... and hier my Identifiable.schema.json

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Identifiable",
  "description": "Identifiable",
  "type": "object",
  "properties": {
    "id": {
      "type": "string",
      "description": "Unique identifier"
    }
  },
  "required": [
    "id"
  ]
}

Exception when I want to delete the file

java.nio.file.FileSystemException: src\test\resources\json\schema\IdentifiableCopy.schema.json: The process cannot access the file because it is being used by another process

I will appreciate if you can solve the problem as soon as possible.

THanks a lot!

@waldimiro Thanks for taking the time to report this
just released version v0.8.3, allow a few minutes for this to get to maven-central

@ivangsa Thanks a lot ! :)