Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to liboscal-java 1.0.2 and addressed changes #91

Merged
merged 2 commits into from
May 27, 2022

Conversation

rgauss
Copy link
Contributor

@rgauss rgauss commented May 11, 2022

No description provided.

@rgauss rgauss requested a review from a team May 11, 2022 20:51
@@ -223,7 +222,14 @@ public Iterable<T> findAll() {
} catch (UnsupportedOperationException | AssertionError e) {
logger.debug("Unparsable content found at {}", filePath);
} catch (IOException e) {
throw new DataRetrievalFailureException("Failure in loading Oscal object.", e);
if (e.getMessage() != null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems potentially brittle but we seem to have lost the more specific exception we previously used.

Copy link

@david-waltermire david-waltermire May 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand what you are saying here. There are three major cases where an IOException can be thrown now.

  1. There is a problem reading from the underlying stream.
  2. The content is malformed JSON.
  3. The content is malformed OSCAL.

In all cases the content cannot be parsed. Previously 1 and 2 caused an IOException and 3 caused a BindingException. Given 2 and 3 are syntax errors, I wanted to normalize the exceptions thrown.

FWIW, there is an IJsonProblemHandler that is used as a callback from the deserializer to address OSCAL syntax issues. I believe I need to expose an option to override this on the deserializer, to allow for detection of unknown and missing content. This might be better than using the exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, there is an IJsonProblemHandler that is used as a callback from the deserializer to address OSCAL syntax issues. I believe I need to expose an option to override this on the deserializer, to allow for detection of unknown and missing content. This might be better than using the exception.

Yeah, saw that, and yes that could be helpful.

We might also take an approach of some simple content detection first rather than jumping straight into trying to load.

public void serialize(@NotNull T data, @NotNull OutputStream os) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(os);
serialize(data, writer);
// Stream is already closed after
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@david-waltermire-nist, this might be of interest. Perhaps the default behavior of Jackson (closing the stream) changed in the update?

Copy link
Contributor Author

@rgauss rgauss May 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add a bit more detail, the part of the tests that were failing essentially just creates a ByteArrayOutputStream and ends up calling ISerializerwhere writer.flush fails.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's weird that this is happening now. It should have happened all along. Looks like Jackson's default behavior is to auto-close the stream.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I change the default behavior? I believe I should.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a simple test in OscalBindingContextTest in liboscal-java that fails with the same error:

  @Test
  void testSerializeSspToOutputStream() throws IOException {
    SystemSecurityPlan ssp = new SystemSecurityPlan();

    IBindingContext context = IBindingContext.newInstance();
    ISerializer<SystemSecurityPlan> serializer = context.newSerializer(Format.JSON, SystemSecurityPlan.class);
    serializer.enableFeature(Feature.SERIALIZE_ROOT);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    serializer.serialize(ssp, out);
  }

That didn't seem worth creating a PR into liboscal-java and the fix into metaschema-java, but I can do that if you'd like.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the bigger concern is that the out-of-the-box metaschema serializer currently fails, regardless of what the calling code would want to do with that stream after serialization.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I'll look into this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I ran your unit test. This is caused by the Jackson autoclose behavior. I am going to disable it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent, thanks!

@david-waltermire
Copy link

@rgauss It looks like the changes here are minimal based on the liboscal-java API adjustments. Glad to see this.

@rgauss rgauss marked this pull request as ready for review May 12, 2022 23:38
@rgauss rgauss requested a review from laurelmay May 13, 2022 11:44
@laurelmay
Copy link
Contributor

@rgauss now that #94 is merged, there's a conflict here and I don't want to guess at resolving it. I think we want:

    this.deserializer = new DefaultJsonDeserializer<T>(context, classBinding);
    this.deserializer.enableFeature(Feature.DESERIALIZE_JSON_ROOT_PROPERTY);

but I'm not 100% sure.

@rgauss rgauss force-pushed the feature/upgrade-liboscaljava-1.0.2 branch from 67020fc to 742ef3a Compare May 27, 2022 12:33
@rgauss
Copy link
Contributor Author

rgauss commented May 27, 2022

@rgauss now that #94 is merged, there's a conflict here and I don't want to guess at resolving it. I think we want:

    this.deserializer = new DefaultJsonDeserializer<T>(context, classBinding);
    this.deserializer.enableFeature(Feature.DESERIALIZE_JSON_ROOT_PROPERTY);

but I'm not 100% sure.

Yes, that was right. Rebased, should be good to go.

@rgauss rgauss requested a review from laurelmay May 27, 2022 12:37
@laurelmay laurelmay requested a review from a team May 27, 2022 12:39
@zclarkEDC zclarkEDC merged commit 9be3770 into develop May 27, 2022
@laurelmay laurelmay deleted the feature/upgrade-liboscaljava-1.0.2 branch May 27, 2022 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants