Skip to content

Commit

Permalink
Fix Jackson max length limitation issue for npm package metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
yma96 committed Mar 22, 2024
1 parent bb59141 commit 3f92b1f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.commonjava.indy.pkg.npm.jaxrs;

import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.binary.Base64;
Expand Down Expand Up @@ -475,6 +476,10 @@ private List<Transfer> generateNPMContentsFromTransfer( final Transfer transfer,
try
{
ObjectMapper mapper = new ObjectMapper();
// Enlarge the max length for a single string value inside of the json
mapper.getFactory()
.setStreamReadConstraints(
StreamReadConstraints.builder().maxStringLength( Integer.MAX_VALUE ).build() );
JsonNode root = mapper.readTree( transfer.openInputStream( true ) );

String versionPath = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
*/
package org.commonjava.indy.pkg.npm.model;

import com.fasterxml.jackson.core.StreamReadConstraints;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.commonjava.indy.model.core.io.IndyObjectMapper;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -72,4 +78,20 @@ public void ignoreCouchDBJsonDataTest() throws Exception
assertThat( jsonResult.contains( "_id" ), equalTo( false ) );
assertThat( jsonResult.contains( "_attachments" ), equalTo( false ) );
}

@Test
public void testLargeJson() throws IOException
{
ObjectMapper mapper = new ObjectMapper();
// read ~40MB json file
try (InputStream input = this.getClass().getClassLoader().getResourceAsStream( "large-stream.json"))
{
mapper.getFactory()
.setStreamReadConstraints(
StreamReadConstraints.builder().maxStringLength( Integer.MAX_VALUE ).build() );
JsonNode root = mapper.readTree( input );
JsonNode idnode = root.path( "_id" );
assertThat( idnode.asText(), equalTo( "@janus-idp/backstage-plugin-orchestrator" ));
}
}
}

Large diffs are not rendered by default.

0 comments on commit 3f92b1f

Please sign in to comment.