Skip to content

Commit

Permalink
chore!: Rename servicePath to basePath (SAP#3484)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomfrenken authored Feb 10, 2023
1 parent a103a12 commit 9896098
Show file tree
Hide file tree
Showing 225 changed files with 379 additions and 386 deletions.
6 changes: 6 additions & 0 deletions .changeset/new-brooms-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sap-cloud-sdk/openapi-generator': major
'@sap-cloud-sdk/generator': major
---

[Compatibility Note] Rename servicePath to basePath.
13 changes: 7 additions & 6 deletions V3-Upgrade-Guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ This document will guide you through the steps necessary to upgrade to version 3
- [Use Service Function instead of API Constructor](#use-service-function-instead-of-api-constructor)
- [Adjust Operation Names Starting With `_` in Generated OData Clients](#adjust-operation-names-starting-with-_-in-generated-odata-clients)
- [Use `optionsPerService` in OData generator](#use-optionsperservice-in-odata-generator)
- [Set `servicePath` in `options-per-service.json`](#set-servicepath-in-options-per-servicejson)
- [Set `basePath` in `options-per-service.json`](#set-servicepath-in-options-per-servicejson)
- [Change `servicePath` to `basePath`]

## Update Your Project Dependencies

Expand Down Expand Up @@ -197,14 +198,14 @@ Set the `optionsPerService` to either:
If the file does not exist, it will be created.
- The directory from which the file is read/created (e.g. `someDir`. This will create a file named `options-per-service.json` in `someDir`)

## Set `servicePath` in `options-per-service.json`
## Set `basePath` in `options-per-service.json`

By default, generation of OData clients will fail if the `servicePath` cannot be determined at the time of generation.
By default, generation of OData clients will fail if the `basePath` cannot be determined at the time of generation.
The generator will determine the path from:
- `servicePath` property in the `options-per-service.json`
- `basePath` property in the `options-per-service.json`
- `.edmx` service specification, or
- swagger file
in the above mentioned order.

To allow generation in spite of missing `servicePath`, set the `skipValidation` option to true.
This will generate the client successfuly with `servicePath` set to `/`.
To allow generation in spite of missing `basePath`, set the `skipValidation` option to true.
This will generate the client successfuly with `basePath` set to `/`.
4 changes: 2 additions & 2 deletions knowledge-base/adr/0021-odata-url-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ People.requestBuilder().someOperation(params); // getAll(), getByKey(...keys), c
which then generates url like below that has only **ONE** level:

```
/servicePath/People // getAll(), create, update, delete(entity)
/servicePath/People(1) // getByKey, delete(key)
/basePath/People // getAll(), create, update, delete(entity)
/basePath/People(1) // getByKey, delete(key)
```

### Exception: `asChildOf`
Expand Down
4 changes: 2 additions & 2 deletions knowledge-base/adr/0024-url-encoding.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ Let's discuss OData path and OpenAPI path separately, as OData case is a bit com
Below is one example about how the path should look like in terms of OData

```
/servicePath/Entity('key')/to_MultiLink('key')/to_SingleLink
/basePath/Entity('key')/to_MultiLink('key')/to_SingleLink
```

- service path: `/servicePath`
- base path: `/basePath`
- entity path: `/Entity(KeyName='value')`
- navigation property path: `/to_MultiLink(KeyName='value')/to_SingleLink`

Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/batch/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function batchSourceFile(
...importBatchDeclarations(service),
batchFunction(service),
changesetFunction(service),
`export const default${service.className}Path = '${service.servicePath}';`,
`export const default${service.className}Path = '${service.basePath}';`,
readRequestType(service),
writeRequestType(service)
]
Expand Down
2 changes: 1 addition & 1 deletion packages/generator/src/entity/class.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('entity class generator generates a class', () => {
expect(staticProperties.map(prop => [prop.name, prop.initializer])).toEqual(
[
['_entityName', `'${breakfastEntity.entitySetName}'`],
['_defaultServicePath', `'${foodService.servicePath}'`],
['_defaultBasePath', `'${foodService.basePath}'`],
['_keys', "['EntityName','BreakfastTime']"]
]
);
Expand Down
8 changes: 4 additions & 4 deletions packages/generator/src/entity/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function staticProperties(
entity: VdmEntity,
service: VdmServiceMetadata
): PropertyDeclarationStructure[] {
return [entityName(entity), defaultServicePath(service), keys(entity)];
return [entityName(entity), defaultBasePath(service), keys(entity)];
}

function entityName(entity: VdmEntity): PropertyDeclarationStructure {
Expand All @@ -75,14 +75,14 @@ function entityName(entity: VdmEntity): PropertyDeclarationStructure {
};
}

function defaultServicePath(
function defaultBasePath(
service: VdmServiceMetadata
): PropertyDeclarationStructure {
return {
kind: StructureKind.Property,
name: prependPrefix('defaultServicePath'),
name: prependPrefix('defaultBasePath'),
isStatic: true,
initializer: `'${service.servicePath}'`,
initializer: `'${service.basePath}'`,
docs: [addLeadingNewline('Default url path for the according service.')]
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('class', () => {
originalFileName: 'API_A_SERV',
directoryName: 'a-serv',
npmPackageName: '@sap/a-serv',
servicePath: '/path/to/serv',
basePath: '/path/to/serv',
complexTypes: [],
enumTypes: [],
entities: [],
Expand Down
10 changes: 5 additions & 5 deletions packages/generator/src/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ describe('generator', () => {
{
API_TEST_SRV: {
directoryName: 'test-service',
servicePath: '/sap/opu/odata/sap/API_TEST_SRV',
basePath: '/sap/opu/odata/sap/API_TEST_SRV',
npmPackageName: 'test-service'
}
},
Expand All @@ -303,7 +303,7 @@ describe('generator', () => {
{
API_TEST_SRV: {
directoryName: 'test-service',
servicePath: '/sap/opu/odata/sap/API_TEST_SRV',
basePath: '/sap/opu/odata/sap/API_TEST_SRV',
npmPackageName: 'test-service'
}
},
Expand All @@ -329,7 +329,7 @@ describe('generator', () => {
{
API_TEST_SRV: {
directoryName: 'test-service',
servicePath: '/sap/opu/odata/sap/API_TEST_SRV',
basePath: '/sap/opu/odata/sap/API_TEST_SRV',
npmPackageName: 'test-service'
}
},
Expand Down Expand Up @@ -359,7 +359,7 @@ describe('generator', () => {
},
API_TEST_SRV: {
directoryName: 'test-service',
servicePath: '/sap/opu/odata/sap/API_TEST_SRV',
basePath: '/sap/opu/odata/sap/API_TEST_SRV',
npmPackageName: 'test-service'
}
},
Expand All @@ -385,7 +385,7 @@ describe('generator', () => {
{
API_TEST_SRV: {
directoryName: 'test-service',
servicePath: '/sap/opu/odata/sap/API_TEST_SRV',
basePath: '/sap/opu/odata/sap/API_TEST_SRV',
npmPackageName: 'test-service'
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/generator/src/global-name-formatter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ describe('global-name-formatter', () => {
const vdmMapping: VdmMapping = {
API_A_SERV: {
directoryName: 'a-serv',
servicePath: '/path/to/serv',
basePath: '/path/to/serv',
npmPackageName: '@sap/a-serv'
},
API_B_SERV: {
directoryName: 'b-serv',
servicePath: '/path/to/serv',
basePath: '/path/to/serv',
npmPackageName: '@sap/b-serv'
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function getRequestBuilderArgumentsBase(
const sharedParameters = [getTransformer(operation), 'params'];
if (!operation.isBound) {
return [
`'${service.servicePath}'`,
`'${service.basePath}'`,
`'${operation.originalName}'`,
...sharedParameters,
'deSerializers'
Expand Down
34 changes: 16 additions & 18 deletions packages/generator/src/options-per-service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createLogger } from '@sap-cloud-sdk/util';
import {
getServicePath,
getBasePath,
OptionsPerService,
optionsPerService,
VdmMapping
Expand All @@ -15,7 +15,7 @@ describe('options-per-service', () => {
originalFileName: 'API_A_SERV',
directoryName: 'a-serv',
npmPackageName: '@sap/a-serv',
servicePath: '/path/to/serv',
basePath: '/path/to/serv',
complexTypes: [],
enumTypes: [],
entities: [],
Expand All @@ -30,7 +30,7 @@ describe('options-per-service', () => {
originalFileName: 'API_B_SERV',
directoryName: 'b-serv',
npmPackageName: '@sap/b-serv',
servicePath: '/path/to/serv',
basePath: '/path/to/serv',
complexTypes: [],
enumTypes: [],
entities: [],
Expand All @@ -45,20 +45,20 @@ describe('options-per-service', () => {
const expectedVdmMapping: VdmMapping = {
API_A_SERV: {
directoryName: 'a-serv',
servicePath: '/path/to/serv',
basePath: '/path/to/serv',
npmPackageName: '@sap/a-serv'
},
API_B_SERV: {
directoryName: 'b-serv',
servicePath: '/path/to/serv',
basePath: '/path/to/serv',
npmPackageName: '@sap/b-serv'
}
};

expect(optionsPerService(serviceMetadata)).toEqual(expectedVdmMapping);
});

it('gets servicePath from optionsPerService over edmx self link and swagger', () => {
it('gets basePath from optionsPerService over edmx self link and swagger', () => {
const metadata = {
edmx: {
path: 'test/path/file.edmx',
Expand All @@ -70,15 +70,15 @@ describe('options-per-service', () => {
};

const optionsPerServiceIn = {
servicePath: '/options-test-service'
basePath: '/options-test-service'
} as OptionsPerService;

expect(getServicePath(metadata, false, optionsPerServiceIn)).toEqual(
expect(getBasePath(metadata, false, optionsPerServiceIn)).toEqual(
'/options-test-service'
);
});

it('gets servicePath from swagger when it cannot be determined from options or self link', () => {
it('gets basePath from swagger when it cannot be determined from options or self link', () => {
const metadata = {
edmx: {
path: 'test/path/file.edmx'
Expand All @@ -88,12 +88,10 @@ describe('options-per-service', () => {
} as any
};

expect(getServicePath(metadata, false)).toEqual(
'/swagger-test-service-path'
);
expect(getBasePath(metadata, false)).toEqual('/swagger-test-service-path');
});

it('should return "/" if skipValidation is true and servicePath cannot be determined from options-per-service, self link and swagger', () => {
it('should return "/" if skipValidation is true and basePath cannot be determined from options-per-service, self link and swagger', () => {
const metadata = {
edmx: {
path: 'test/path/file.edmx'
Expand All @@ -106,21 +104,21 @@ describe('options-per-service', () => {
messageContext: 'options-per-service'
});
const warnSpy = jest.spyOn(logger, 'warn');
expect(getServicePath(metadata, true)).toEqual('/');
expect(getBasePath(metadata, true)).toEqual('/');
expect(warnSpy).toHaveBeenCalledWith(
'[ file ] No service path could be determined from available metadata! Setting "servicePath" to "/" in the "options-per-service.json".'
'[ file ] No base path could be determined from available metadata! Setting "basePath" to "/" in the "options-per-service.json".'
);
});

it('should throw if skipValidation is false and servicePath cannot be determined from options-per-service, self link and swagger', () => {
it('should throw if skipValidation is false and basePath cannot be determined from options-per-service, self link and swagger', () => {
const metadata = {
edmx: {
path: 'test/path/file.edmx'
} as any,
swagger: {} as any
};
expect(() => getServicePath(metadata, false)).toThrowError(
/No service path could be determined from available metadata!/
expect(() => getBasePath(metadata, false)).toThrowError(
/No base path could be determined from available metadata!/
);
});
});
28 changes: 14 additions & 14 deletions packages/generator/src/options-per-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { existsSync, readFileSync } from 'fs';
import { unixEOL, createLogger } from '@sap-cloud-sdk/util';
import { ParsedGeneratorOptions } from './options';
import { VdmServiceMetadata } from './vdm-types';
import { servicePathFromSwagger } from './swagger-parser/swagger-util';
import { basePathFromSwagger } from './swagger-parser/swagger-util';
import { ServiceMetadata } from './edmx-parser/edmx-file-reader';

const logger = createLogger({
Expand All @@ -28,7 +28,7 @@ export interface OptionsPerService {
/**
* @internal
*/
servicePath: string;
basePath: string;
/**
* @internal
*/
Expand All @@ -52,7 +52,7 @@ export function optionsPerService(services: VdmServiceMetadata[]): VdmMapping {
return services.reduce((vdmMapping, service) => {
vdmMapping[service.originalFileName] = {
directoryName: service.directoryName,
servicePath: service.servicePath,
basePath: service.basePath,
npmPackageName: service.npmPackageName
};

Expand All @@ -68,35 +68,35 @@ export function optionsPerServiceFile(services: VdmServiceMetadata[]): string {
/**
* @internal
*/
export function getServicePath(
export function getBasePath(
metadata: ServiceMetadata,
skipValidation: boolean,
optionsPerServiceIn?: OptionsPerService
): string {
let servicePath =
optionsPerServiceIn?.servicePath ||
servicePathFromSelfLink(metadata.edmx.selfLink) ||
servicePathFromSwagger(metadata.swagger);
if (!servicePath) {
let basePath =
optionsPerServiceIn?.basePath ||
basePathFromSelfLink(metadata.edmx.selfLink) ||
basePathFromSwagger(metadata.swagger);
if (!basePath) {
if (skipValidation) {
logger.warn(
`[ ${
parse(metadata.edmx.path.toString()).name
} ] No service path could be determined from available metadata! Setting "servicePath" to "/" in the "options-per-service.json".`
} ] No base path could be determined from available metadata! Setting "basePath" to "/" in the "options-per-service.json".`
);
servicePath = '/';
basePath = '/';
} else {
throw new Error(
`[ ${
parse(metadata.edmx.path.toString()).name
} ] No service path could be determined from available metadata! Set "servicePath" in the "options-per-service.json" to generate client.`
} ] No base path could be determined from available metadata! Set "basePath" in the "options-per-service.json" to generate client.`
);
}
}
return servicePath;
return basePath;
}

function servicePathFromSelfLink(
function basePathFromSelfLink(
selfLink: string | undefined
): string | undefined {
if (selfLink) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function getTestService(npmPackageName?: string): VdmServiceMetadata {
new GlobalNameFormatter({
API_TEST_SRV: {
directoryName: 'test-service',
servicePath: '/sap/opu/odata/sap/API_TEST_SERVICE_SRV;v=0002',
basePath: '/sap/opu/odata/sap/API_TEST_SERVICE_SRV;v=0002',
npmPackageName: npmPackageName || '@sap/cloud-sdk-vdm-test-service'
}
})
Expand Down
Loading

0 comments on commit 9896098

Please sign in to comment.