Skip to content

Commit

Permalink
Handle invalid URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
phillip-kruger committed Jun 4, 2024
1 parent e21f4fb commit e8936fd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/mvnpm/npm/model/Package.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public record Package(
String description,
@JsonDeserialize(using = LicenseDeserializer.class) License license,
@JsonDeserialize(using = AuthorDeserializer.class) Author author,
URL homepage,
@JsonDeserialize(using = URLDeserializer.class) URL homepage,
@JsonDeserialize(using = RepositoryDeserializer.class) Repository repository,
@JsonDeserialize(using = BugsDeserializer.class) Bugs bugs,
String main,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/mvnpm/npm/model/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record Project(
@JsonProperty("name") Name name,
String description,
@JsonProperty("dist-tags") DistTags distTags,
URL homepage,
@JsonDeserialize(using = URLDeserializer.class) URL homepage,
@JsonDeserialize(using = LicenseDeserializer.class) License license,
@JsonDeserialize(using = VersionDeserializer.class) Set<String> versions,
Map<String, String> time) {
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/io/mvnpm/npm/model/URLDeserializer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.mvnpm.npm.model;

import java.io.IOException;
import java.net.URL;

import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;

/**
* To handle some invalid URL values
*
* @author Phillip Kruger ([email protected])
*/
public class URLDeserializer extends StdDeserializer<URL> {

public URLDeserializer() {
this(null);
}

public URLDeserializer(Class<?> vc) {
super(vc);
}

@Override
public URL deserialize(JsonParser jp, DeserializationContext dc) throws IOException, JacksonException {
try {
return jp.readValueAs(URL.class);
} catch (Throwable t) {
return null;
}
}

}
11 changes: 9 additions & 2 deletions src/main/resources/web/app/mvnpm-home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,8 +396,7 @@ export class MvnpmHome extends LitElement {
<div class="searchResultContent" slot="content">
<div class="searchResultDetails">
<div class="searchResultDescription">
${unsafeHTML(marked(result.package.description))}
${this._renderBy(result)}
${this._renderPackageDescription(result)}
</div>
<div class="searchResultLinks">
<a href="${result.package.links.npm}" target="_blank">
Expand All @@ -417,6 +416,14 @@ export class MvnpmHome extends LitElement {
}
}

_renderPackageDescription(result){
if(result.package.description){
return html`
${unsafeHTML(marked(result.package.description))}
${this._renderBy(result)}`;
}
}

_renderBy(result){
if(result.package.author && result.package.author.name){
return html` - by ${result.package.author.name}`;
Expand Down

0 comments on commit e8936fd

Please sign in to comment.