Skip to content

Commit

Permalink
FIX multiple type of npm bin fieldd deserialization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
yma96 committed Mar 1, 2024
1 parent 1b8640b commit 243230c
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.commonjava.indy.pkg.npm.content;

import org.apache.commons.io.IOUtils;
import org.commonjava.cdi.util.weft.PoolWeftExecutorService;
import org.commonjava.cdi.util.weft.WeftExecutorService;
import org.commonjava.indy.audit.ChangeSummary;
Expand Down Expand Up @@ -54,7 +53,6 @@

import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

Expand Down Expand Up @@ -147,6 +145,47 @@ public void generateMetadataWhenMissing() throws Exception
assertNotNull(after);
}

@Test
public void testSingleBinFieldWhenGenerateFromTarball() throws Exception
{
ChangeSummary summary = new ChangeSummary( "test","Init NPM hosted repo." );
final HostedRepository hostedRepository = new HostedRepository( NPM_PKG_KEY, "npm-builds" );
initStore(hostedRepository, summary);

final KeyedLocation location = LocationUtils.toLocation( hostedRepository );

storeFile( location, "jquery/-/jquery-9.0.5.tgz", "tarball/version-bin-1.tgz");
storeFile( location, "jquery/-/jquery-9.0.6.tgz", "tarball/version-bin-2.tgz");
storeFile( location, "jquery/9.0.5", "metadata/version-bin-1.json" );
storeFile( location, "jquery/9.0.6", "metadata/version-bin-2.json" );

final String jqueryMetadataPath = "jquery/package.json";

// Check the package metadata before generation.
Transfer before = fileManager.retrieve( hostedRepository, jqueryMetadataPath );
assertNull(before);

Transfer metadataFile = generator.generateFileContent( hostedRepository, jqueryMetadataPath, new EventMetadata( ) );
assertNotNull(metadataFile);

final IndyObjectMapper mapper = new IndyObjectMapper( true );
try ( InputStream input = metadataFile.openInputStream() )
{
PackageMetadata packageMetadata = mapper.readValue( input, PackageMetadata.class );

assertNotNull( packageMetadata );
assertEquals( 2, packageMetadata.getVersions().size() );
assertEquals( 1, packageMetadata.getVersions().get( "9.0.5" ).getBin().size() );
assertEquals( 1, packageMetadata.getVersions().get( "9.0.6" ).getBin().size() );
assertEquals("./lib/json.js", packageMetadata.getVersions().get("9.0.5").getBin().get( "json" ));
assertEquals("./lib/json.js", packageMetadata.getVersions().get("9.0.6").getBin().get( "json" ));
}

// Check the package metadata after generation.
Transfer after = fileManager.retrieve( hostedRepository, jqueryMetadataPath );
assertNotNull(after);
}

@Test
public void generateMetadataWhenMissingForScoped() throws Exception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "json",
"description": "a 'json' command for massaging and processing JSON on the command line",
"version": "9.0.5",
"repository": {
"type": "git",
"url": "git://github.com/trentm/json.git"
},
"author": "Trent Mick <[email protected]> (http://trentm.com)",
"main": "./lib/json.js",
"directories": {
"man": "./man/man1"
},
"bin": "./lib/json.js",
"scripts": {
"test": "make test"
},
"engines": {
"node": ">=0.10.0"
},
"keywords": ["json", "jsontool", "filter", "command", "shell"],
"devDependencies": {
"uglify-js": "1.1.x",
"nodeunit": "0.8.x",
"ansidiff": "1.0",
"ben": "0.0.x",
"async": "0.1.22",
"semver": "1.1.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "json",
"description": "a 'json' command for massaging and processing JSON on the command line",
"version": "9.0.6",
"repository": {
"type": "git",
"url": "git://github.com/trentm/json.git"
},
"author": "Trent Mick <[email protected]> (http://trentm.com)",
"main": "./lib/json.js",
"man": ["./man/man1/json.1"],
"bin": "./lib/json.js",
"scripts": {
"test": "make test"
},
"engines": {
"node": ">=0.10.0"
},
"keywords": ["json", "jsontool", "filter", "command", "shell"],
"devDependencies": {
"uglify-js": "1.1.x",
"nodeunit": "0.8.x",
"ansidiff": "1.0",
"ben": "0.0.x",
"async": "0.1.22",
"semver": "1.1.0"
}
}
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,27 @@
*/
package org.commonjava.indy.pkg.npm.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.commonjava.indy.pkg.npm.model.converter.ObjectToBinConverter;
import org.commonjava.indy.pkg.npm.model.converter.ObjectToLicenseConverter;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import static org.commonjava.indy.pkg.npm.model.converter.ObjectToBinConverter.SINGLE_BIN;

@ApiModel( description = "Specify all the corresponding versions metadata for the package." )
public class VersionMetadata
implements Serializable, Comparable<VersionMetadata>
implements Serializable, Comparable<VersionMetadata>
{
private static final long serialVersionUID = 1L;

@ApiModelProperty( required = true, dataType = "String", value = "The name and version together form an identifier that is assumed to be completely unique." )
@ApiModelProperty( required = true, dataType = "String",
value = "The name and version together form an identifier that is assumed to be completely unique." )
private String name;

private String title;
Expand All @@ -41,7 +44,8 @@ public class VersionMetadata

private String main;

@ApiModelProperty( required = true, dataType = "String", value = "The name and version together form an identifier that is assumed to be completely unique." )
@ApiModelProperty( required = true, dataType = "String",
value = "The name and version together form an identifier that is assumed to be completely unique." )
private String version;

private String url;
Expand All @@ -59,25 +63,28 @@ public class VersionMetadata
@ApiModelProperty( required = false, dataType = "Repository", value = "Specify the place where your code lives." )
private Repository repository;

@ApiModelProperty( required = false, dataType = "Bugs", value = "The issue tracker and / or the email address to which issues should be reported." )
@ApiModelProperty( required = false, dataType = "Bugs",
value = "The issue tracker and / or the email address to which issues should be reported." )
private Bugs bugs;

@Deprecated
@ApiModelProperty( value = "These styles are now deprecated. Instead, use SPDX expressions." )
private List<License> licenses;

@JsonDeserialize(converter = ObjectToLicenseConverter.class)
@JsonDeserialize( converter = ObjectToLicenseConverter.class )
private License license;

private Map<String, String> dependencies;

private Map<String, Object> devDependencies;

@JsonDeserialize( converter = ObjectToBinConverter.class )
private Map<String, String> bin;

private Map<String, String> jsdomVersions;

@ApiModelProperty( required = false, dataType = "Map", allowableValues = "prepare:<script>, build:<script>, start:<script>, test:<script>, precommit:<script>, commitmsg:<script>, etc." )
@ApiModelProperty( required = false, dataType = "Map",
allowableValues = "prepare:<script>, build:<script>, start:<script>, test:<script>, precommit:<script>, commitmsg:<script>, etc." )
private Map<String, Object> scripts;

private Dist dist;
Expand Down Expand Up @@ -308,8 +315,24 @@ public Map<String, Object> getDevDependencies()
return devDependencies;
}

public void setDevDependencies( Map<String, Object> devDependencies )
{
this.devDependencies = devDependencies;
}

public Map<String, String> getBin()
{
if ( null == bin )
{
return null;
}
String value = bin.get( SINGLE_BIN );
if ( null != value )
{
bin.remove( SINGLE_BIN );
// ref https://docs.npmjs.com/cli/v7/configuring-npm/package-json#bin
bin.put( name, value );
}
return bin;
}

Expand All @@ -318,11 +341,6 @@ public void setBin( Map<String, String> bin )
this.bin = bin;
}

public void setDevDependencies( Map<String, Object> devDependencies )
{
this.devDependencies = devDependencies;
}

public Map<String, String> getJsdomVersions()
{
return jsdomVersions;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Copyright (C) 2011-2023 Red Hat, Inc. (https://github.com/Commonjava/indy)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.commonjava.indy.pkg.npm.model.converter;

import com.fasterxml.jackson.databind.util.StdConverter;

import java.util.HashMap;
import java.util.Map;

public class ObjectToBinConverter
extends StdConverter<Object, Map<String, String>>
{

public static final String SINGLE_BIN = "SINGLE_BIN";

@Override
public Map<String, String> convert( Object o )
{
if ( o instanceof Map )
{
return (Map<String, String>) o;
}
// Use SPDX expressions, ref https://docs.npmjs.com/cli/v7/configuring-npm/package-json
// parse String value to Map value
Map<String, String> result = new HashMap<>();
result.put( SINGLE_BIN, o.toString() );
return result;
}
}

0 comments on commit 243230c

Please sign in to comment.