Skip to content

Commit 8a3b3ea

Browse files
authored
Merge pull request #2417 from yma96/master
FIX multiple type of npm bin fieldd deserialization issue
2 parents 1b8640b + 243230c commit 8a3b3ea

File tree

7 files changed

+171
-14
lines changed

7 files changed

+171
-14
lines changed

Diff for: addons/pkg-npm/common/src/test/java/org/commonjava/indy/pkg/npm/content/PackageMetadataGeneratorTest.java

+41-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.commonjava.indy.pkg.npm.content;
1717

18-
import org.apache.commons.io.IOUtils;
1918
import org.commonjava.cdi.util.weft.PoolWeftExecutorService;
2019
import org.commonjava.cdi.util.weft.WeftExecutorService;
2120
import org.commonjava.indy.audit.ChangeSummary;
@@ -54,7 +53,6 @@
5453

5554
import java.io.InputStream;
5655
import java.util.Collections;
57-
import java.util.Map;
5856
import java.util.concurrent.Executors;
5957
import java.util.concurrent.ThreadPoolExecutor;
6058

@@ -147,6 +145,47 @@ public void generateMetadataWhenMissing() throws Exception
147145
assertNotNull(after);
148146
}
149147

148+
@Test
149+
public void testSingleBinFieldWhenGenerateFromTarball() throws Exception
150+
{
151+
ChangeSummary summary = new ChangeSummary( "test","Init NPM hosted repo." );
152+
final HostedRepository hostedRepository = new HostedRepository( NPM_PKG_KEY, "npm-builds" );
153+
initStore(hostedRepository, summary);
154+
155+
final KeyedLocation location = LocationUtils.toLocation( hostedRepository );
156+
157+
storeFile( location, "jquery/-/jquery-9.0.5.tgz", "tarball/version-bin-1.tgz");
158+
storeFile( location, "jquery/-/jquery-9.0.6.tgz", "tarball/version-bin-2.tgz");
159+
storeFile( location, "jquery/9.0.5", "metadata/version-bin-1.json" );
160+
storeFile( location, "jquery/9.0.6", "metadata/version-bin-2.json" );
161+
162+
final String jqueryMetadataPath = "jquery/package.json";
163+
164+
// Check the package metadata before generation.
165+
Transfer before = fileManager.retrieve( hostedRepository, jqueryMetadataPath );
166+
assertNull(before);
167+
168+
Transfer metadataFile = generator.generateFileContent( hostedRepository, jqueryMetadataPath, new EventMetadata( ) );
169+
assertNotNull(metadataFile);
170+
171+
final IndyObjectMapper mapper = new IndyObjectMapper( true );
172+
try ( InputStream input = metadataFile.openInputStream() )
173+
{
174+
PackageMetadata packageMetadata = mapper.readValue( input, PackageMetadata.class );
175+
176+
assertNotNull( packageMetadata );
177+
assertEquals( 2, packageMetadata.getVersions().size() );
178+
assertEquals( 1, packageMetadata.getVersions().get( "9.0.5" ).getBin().size() );
179+
assertEquals( 1, packageMetadata.getVersions().get( "9.0.6" ).getBin().size() );
180+
assertEquals("./lib/json.js", packageMetadata.getVersions().get("9.0.5").getBin().get( "json" ));
181+
assertEquals("./lib/json.js", packageMetadata.getVersions().get("9.0.6").getBin().get( "json" ));
182+
}
183+
184+
// Check the package metadata after generation.
185+
Transfer after = fileManager.retrieve( hostedRepository, jqueryMetadataPath );
186+
assertNotNull(after);
187+
}
188+
150189
@Test
151190
public void generateMetadataWhenMissingForScoped() throws Exception
152191
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "json",
3+
"description": "a 'json' command for massaging and processing JSON on the command line",
4+
"version": "9.0.5",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/trentm/json.git"
8+
},
9+
"author": "Trent Mick <[email protected]> (http://trentm.com)",
10+
"main": "./lib/json.js",
11+
"directories": {
12+
"man": "./man/man1"
13+
},
14+
"bin": "./lib/json.js",
15+
"scripts": {
16+
"test": "make test"
17+
},
18+
"engines": {
19+
"node": ">=0.10.0"
20+
},
21+
"keywords": ["json", "jsontool", "filter", "command", "shell"],
22+
"devDependencies": {
23+
"uglify-js": "1.1.x",
24+
"nodeunit": "0.8.x",
25+
"ansidiff": "1.0",
26+
"ben": "0.0.x",
27+
"async": "0.1.22",
28+
"semver": "1.1.0"
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "json",
3+
"description": "a 'json' command for massaging and processing JSON on the command line",
4+
"version": "9.0.6",
5+
"repository": {
6+
"type": "git",
7+
"url": "git://github.com/trentm/json.git"
8+
},
9+
"author": "Trent Mick <[email protected]> (http://trentm.com)",
10+
"main": "./lib/json.js",
11+
"man": ["./man/man1/json.1"],
12+
"bin": "./lib/json.js",
13+
"scripts": {
14+
"test": "make test"
15+
},
16+
"engines": {
17+
"node": ">=0.10.0"
18+
},
19+
"keywords": ["json", "jsontool", "filter", "command", "shell"],
20+
"devDependencies": {
21+
"uglify-js": "1.1.x",
22+
"nodeunit": "0.8.x",
23+
"ansidiff": "1.0",
24+
"ben": "0.0.x",
25+
"async": "0.1.22",
26+
"semver": "1.1.0"
27+
}
28+
}
Binary file not shown.
Binary file not shown.

Diff for: addons/pkg-npm/model-java/src/main/java/org/commonjava/indy/pkg/npm/model/VersionMetadata.java

+30-12
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,27 @@
1515
*/
1616
package org.commonjava.indy.pkg.npm.model;
1717

18-
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
1918
import com.fasterxml.jackson.annotation.JsonProperty;
2019
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
2120
import io.swagger.annotations.ApiModel;
2221
import io.swagger.annotations.ApiModelProperty;
22+
import org.commonjava.indy.pkg.npm.model.converter.ObjectToBinConverter;
2323
import org.commonjava.indy.pkg.npm.model.converter.ObjectToLicenseConverter;
2424

2525
import java.io.Serializable;
2626
import java.util.List;
2727
import java.util.Map;
2828

29+
import static org.commonjava.indy.pkg.npm.model.converter.ObjectToBinConverter.SINGLE_BIN;
30+
2931
@ApiModel( description = "Specify all the corresponding versions metadata for the package." )
3032
public class VersionMetadata
31-
implements Serializable, Comparable<VersionMetadata>
33+
implements Serializable, Comparable<VersionMetadata>
3234
{
3335
private static final long serialVersionUID = 1L;
3436

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

3841
private String title;
@@ -41,7 +44,8 @@ public class VersionMetadata
4144

4245
private String main;
4346

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

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

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

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

69-
@JsonDeserialize(converter = ObjectToLicenseConverter.class)
74+
@JsonDeserialize( converter = ObjectToLicenseConverter.class )
7075
private License license;
7176

7277
private Map<String, String> dependencies;
7378

7479
private Map<String, Object> devDependencies;
7580

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

7884
private Map<String, String> jsdomVersions;
7985

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

8390
private Dist dist;
@@ -308,8 +315,24 @@ public Map<String, Object> getDevDependencies()
308315
return devDependencies;
309316
}
310317

318+
public void setDevDependencies( Map<String, Object> devDependencies )
319+
{
320+
this.devDependencies = devDependencies;
321+
}
322+
311323
public Map<String, String> getBin()
312324
{
325+
if ( null == bin )
326+
{
327+
return null;
328+
}
329+
String value = bin.get( SINGLE_BIN );
330+
if ( null != value )
331+
{
332+
bin.remove( SINGLE_BIN );
333+
// ref https://docs.npmjs.com/cli/v7/configuring-npm/package-json#bin
334+
bin.put( name, value );
335+
}
313336
return bin;
314337
}
315338

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

321-
public void setDevDependencies( Map<String, Object> devDependencies )
322-
{
323-
this.devDependencies = devDependencies;
324-
}
325-
326344
public Map<String, String> getJsdomVersions()
327345
{
328346
return jsdomVersions;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* Copyright (C) 2011-2023 Red Hat, Inc. (https://github.com/Commonjava/indy)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.indy.pkg.npm.model.converter;
17+
18+
import com.fasterxml.jackson.databind.util.StdConverter;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
public class ObjectToBinConverter
24+
extends StdConverter<Object, Map<String, String>>
25+
{
26+
27+
public static final String SINGLE_BIN = "SINGLE_BIN";
28+
29+
@Override
30+
public Map<String, String> convert( Object o )
31+
{
32+
if ( o instanceof Map )
33+
{
34+
return (Map<String, String>) o;
35+
}
36+
// Use SPDX expressions, ref https://docs.npmjs.com/cli/v7/configuring-npm/package-json
37+
// parse String value to Map value
38+
Map<String, String> result = new HashMap<>();
39+
result.put( SINGLE_BIN, o.toString() );
40+
return result;
41+
}
42+
}

0 commit comments

Comments
 (0)