Skip to content

Commit

Permalink
Merge pull request #234 from solace-iot-team/fix-apiproducts-legacy-m…
Browse files Browse the repository at this point in the history
…issing-versioninfo

Fix apiproducts legacy missing versioninfo
  • Loading branch information
195858 authored Apr 25, 2022
2 parents 8238c7e + 018d2da commit 3865305
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
3 changes: 3 additions & 0 deletions api-implementation/server/api/services/apiProducts.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export class ApiProductsService {
if (!apiReferenceCheck)
throw new ErrorResponseInternal(422, `Reference check failed ${apiReferenceCheck}`);
const currentState: APIProduct = await this.persistenceService.byName(name);
if (!currentState.meta){
currentState.meta = Versioning.createMeta();
}
if (!Versioning.validateNewVersion(body.meta, currentState.meta)) {
throw new ErrorResponseInternal(409, `Version supplied in meta element is not greater than current version`);
}
Expand Down
52 changes: 27 additions & 25 deletions api-implementation/server/common/versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,42 @@ export class Versioning {
return current + 1;
}

public static isRecognizedVersion(version : string): boolean{
const isSemVer: boolean = semver.parse(version)!=null;
const isInteger:boolean = !isNaN(version as any);
public static isRecognizedVersion(version: string): boolean {
const isSemVer: boolean = semver.parse(version) != null;
const isInteger: boolean = !isNaN(version as any);
return isSemVer || isInteger;
}

public static validateNewVersionString(newVersion: string, previousVersion: string): boolean {
// version is mandatory, do not accept null values
if (!previousVersion || !newVersion){
if (!previousVersion || !newVersion) {
return false;
}
const isSemVer: boolean = (semver.parse(newVersion)!=null && semver.parse(previousVersion)!=null);
const isInteger:boolean = !isNaN(newVersion as any) && !isNaN(previousVersion as any);
if (isSemVer){
const isSemVer: boolean = (semver.parse(newVersion) != null && semver.parse(previousVersion) != null);
const isInteger: boolean = !isNaN(newVersion as any) && !isNaN(previousVersion as any);
if (isSemVer) {
return semver.gt(newVersion, previousVersion);
} else if (isInteger){
return parseInt(newVersion)> parseInt(previousVersion);
} else {
} else if (isInteger) {
return parseInt(newVersion) > parseInt(previousVersion);
} else {
return (newVersion != previousVersion);
}
}
public static validateNewVersion(newVersion: Meta, previousVersion: Meta): boolean {
if (!previousVersion && !newVersion){
if (!previousVersion && !newVersion) {
return true;
}
// if the caller has not provided a new version and it was set manually before we reject the request
if (!newVersion && previousVersion && previousVersion.version != Versioning.INITIAL_VERSION) {
return false;
}
}
// if the caller has not supplied a new version and it was not set manually before we accept the request
if (!newVersion && previousVersion && previousVersion.version == Versioning.INITIAL_VERSION) {
if (!newVersion && previousVersion && previousVersion.version == Versioning.INITIAL_VERSION) {
return true;
}
let semVerPrev = (previousVersion && previousVersion.version == Versioning.INITIAL_VERSION) ? `1.${previousVersion[Versioning.INTERNAL_REVISION]}.0` : previousVersion.version;
let semVerPrev = (previousVersion && previousVersion.version == Versioning.INITIAL_VERSION) ?
`1.${previousVersion[Versioning.INTERNAL_REVISION]}.0` :
(previousVersion && previousVersion.version)?previousVersion.version:Versioning.INITIAL_VERSION;
return Versioning.validateNewVersionString(newVersion.version, semVerPrev);
}
public static createMeta(version?: string): Meta {
Expand All @@ -67,24 +69,24 @@ export class Versioning {
const user: string = ns.getStore().get(ContextConstants.AUTHENTICATED_USER);
const m: Meta = {
version: (newMeta && newMeta.version) ? newMeta.version : Versioning.INITIAL_VERSION,
created: previousMeta?previousMeta.created:ts,
created: previousMeta ? previousMeta.created : ts,
lastModified: ts,
createdBy: previousMeta?previousMeta.createdBy:user,
createdBy: previousMeta ? previousMeta.createdBy : user,
lastModifiedBy: user,

};
m[Versioning.INTERNAL_REVISION] = Versioning.nextRevision(previousMeta?previousMeta[Versioning.INTERNAL_REVISION]:Versioning.INITIAL_REVISION as number);
m[Versioning.INTERNAL_REVISION] = Versioning.nextRevision(previousMeta ? previousMeta[Versioning.INTERNAL_REVISION] : Versioning.INITIAL_REVISION as number);
return m;
}

public static toExternalRepresentation(meta: Meta): Meta{
if (meta && meta.version && meta.version == Versioning.INITIAL_VERSION) {
meta.version = `1.${meta[Versioning.INTERNAL_REVISION]}.0`
}
if (meta){
delete meta[Versioning.INTERNAL_REVISION];
}
return meta;
public static toExternalRepresentation(meta: Meta): Meta {
if (meta && meta.version && meta.version == Versioning.INITIAL_VERSION) {
meta.version = `1.${meta[Versioning.INTERNAL_REVISION]}.0`
}
if (meta) {
delete meta[Versioning.INTERNAL_REVISION];
}
return meta;
}

public static createRevisionId(name: string, version: string): string {
Expand Down

0 comments on commit 3865305

Please sign in to comment.