Skip to content

JSON schema for events must be loaded from classpath #71

Closed

Description

@aalmiray

The current code foe each event loads its schema in this way

public String eventSchema() {
try {
return Files.readString(Paths.get(CDEventConstants.SCHEMA_FOLDER + "/pipelinerunstarted.json"));
} catch (IOException e) {
throw new CDEventsException("Exception while reading Event JsonSchema file ", e);
}
}

Which means it expects to find the schema as a file in the filesystem. This code fails when the cdevents-java-sdk is consumed as a dependency by other projects, as the schema files are no longer found in the filesystem but rather as embedded resources within the cdevents-java-sdk JAR.

This code must be updated to find the schema as a classpath resource first, falling back to filesystem if not found.

Activity

  1. aalmiray commented on Feb 13, 2024

    @aalmiray
    ContributorAuthor

    Here's an stacktrace that occurs when an event is created with the following code:

            PipelinerunStartedCDEvent cdEvent = new PipelinerunStartedCDEvent();
            cdEvent.setSource(URI.create(source));
            cdEvent.setSubjectId(subjectId);
            cdEvent.setSubjectSource(subjectSource);
            cdEvent.setSubjectUrl(subjectUrl);
            cdEvent.setSubjectPipelineName(subjectPipelineName);
            sendEvent(context, CDEvents.cdEventAsCloudEvent(cdEvent));
    dev.cdevents.exception.CDEventsException: Exception while reading Event JsonSchema file 
    	at dev.cdevents.events.PipelinerunStartedCDEvent.eventSchema(PipelinerunStartedCDEvent.java:106)
    	at dev.cdevents.CDEvents.getJsonSchemaValidationMessages(CDEvents.java:136)
    	at dev.cdevents.CDEvents.validateCDEvent(CDEvents.java:82)
    	at dev.cdevents.CDEvents.cdEventAsCloudEvent(CDEvents.java:55)
    	at org.jreleaser.extensions.cdevents.CDEventsWorkflowListener.onSessionStart(CDEventsWorkflowListener.java:181)
    	at org.jreleaser.model.internal.JReleaserContext.fireSessionStartEvent(JReleaserContext.java:1051)
    	at org.jreleaser.workflow.WorkflowImpl.doExecute(WorkflowImpl.java:104)
    	at org.jreleaser.workflow.WorkflowImpl.execute(WorkflowImpl.java:54)
    	at org.jreleaser.cli.Assemble.doExecute(Assemble.java:99)
    	at org.jreleaser.cli.AbstractModelCommand.execute(AbstractModelCommand.java:89)
    	at org.jreleaser.cli.AbstractCommand.call(AbstractCommand.java:52)
    	at org.jreleaser.cli.AbstractModelCommand.call(AbstractModelCommand.java:53)
    	at org.jreleaser.cli.AbstractCommand.call(AbstractCommand.java:37)
    	at picocli.CommandLine.executeUserObject(CommandLine.java:2041)
    	at picocli.CommandLine.access$1500(CommandLine.java:148)
    	at picocli.CommandLine$RunLast.executeUserObjectOfLastSubcommandWithSameParent(CommandLine.java:2461)
    	at picocli.CommandLine$RunLast.handle(CommandLine.java:2453)
    	at picocli.CommandLine$RunLast.handle(CommandLine.java:2415)
    	at picocli.CommandLine$AbstractParseResultHandler.execute(CommandLine.java:2273)
    	at picocli.CommandLine$RunLast.execute(CommandLine.java:2417)
    	at picocli.CommandLine.execute(CommandLine.java:2170)
    	at org.jreleaser.cli.Main.execute(Main.java:98)
    	at org.jreleaser.cli.Main.run(Main.java:81)
    	at org.jreleaser.cli.Main.main(Main.java:70)
    Caused by: java.nio.file.NoSuchFileException: ../spec/schemas/pipelinerunstarted.json
    	at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
    	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:106)
    	at java.base/sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:111)
    	at java.base/sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:261)
    	at java.base/java.nio.file.Files.newByteChannel(Files.java:379)
    	at java.base/java.nio.file.Files.newByteChannel(Files.java:431)
    	at java.base/java.nio.file.Files.readAllBytes(Files.java:3268)
    	at java.base/java.nio.file.Files.readString(Files.java:3346)
    	at java.base/java.nio.file.Files.readString(Files.java:3305)
    	at dev.cdevents.events.PipelinerunStartedCDEvent.eventSchema(PipelinerunStartedCDEvent.java:104)
    	... 23 more
    
  2. added 4 commits that reference this issue on Feb 13, 2024
    6dbd583
    53f4f0f
    9938a5f
    39cf879
  3. added a commit that references this issue on Feb 14, 2024
    545d69f
  4. added
    released
    on Mar 12, 2024
  5. cdevents-bot commented on Mar 12, 2024

    @cdevents-bot
    Collaborator

    🎉 This issue has been resolved in v0.3.1 (Release Notes)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    released

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions