Skip to content

Commit d67915f

Browse files
committed
Release v2.1.0 update
1 parent f4f0f12 commit d67915f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+4704
-784
lines changed

Documentation/BasicUsage.md

+4
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ p = PetStore.models.Pet('{"name": "Foo"}',"name","Bar");
360360
361361
The final Pet instance will have `name` = `"Bar"` and not `"Foo"`.
362362
363+
### Model inheritance
364+
365+
In the example discussed on this documentation page, there is no inheritance between models and there is no polymorphism in the operations. These feature may be used in more complex services though and this will influence model behavior. If working with such services/specs, see [](./InheritanceAndPolymorphism.md) for more information.
366+
363367
## Example method call with model as input
364368
365369
So finally to call the `addPet` operation with a `Pet` as input:

Documentation/BuildClient.md

+32-29
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
## Generating a MATLAB client using a builder
44

5-
A builder class is provided to simplify the process of building a client, e.g.:
5+
A builder class is provided to simplify the process of building a client.
6+
At a high-level the MATLAB client provides an interface to the Java based generation code.
7+
Previously this approach used Node.js and `npx`, this is no longer the case.
8+
9+
Usage example:
610

711
```matlab
812
% Run startup to configure the package's MATLAB paths
@@ -36,7 +40,11 @@ Additional builder properties can be used to set non default properties:
3640
* templateDir: set the path to the Mustache files, e.g. if providing a customized version of those provided with the package.
3741
* jarPath: set the path to an alternative jar file, the default is `Software/MATLAB/lib/jar/MATLABClientCodegen-openapi-generator-0.0.1.jar`.
3842

39-
The builder will automatically install the `@openapitools/openapi-generator-cli` using `npx` if it is not already present.
43+
See [Generator Configuration Options](./Options.md) for a complete overview of all options.
44+
45+
```{hint}
46+
If the API specification is spread across multiple separate files, instead of specifying `inputSpec` (which can only point to a single file), it is also possible to use `inputSpecRootDirectory` instead. This can point to a whole directory with YAML and JSON files and this will then generate a single MATLAB client which covers all APIs and Models from all files combined.
47+
```
4048

4149
A build log file is produced in the output directory.
4250

@@ -49,23 +57,13 @@ It can be enabled as follows. This can be useful when debugging or first evaluat
4957
c.skipValidateSpec = false;
5058
```
5159

52-
## node and npx configuration
53-
54-
At a high-level the MATLAB client provides an interface to the node (Node.js) based openapitools frontend to the Java based generation code and npx is used to manage the execution of the node package. Thus both node and npx must be available, recent versions of node include npx.
55-
Particularly on Linux systems it is common for a system to have a default install of node which may be significantly older than user installed versions.
56-
To use a specific version of node e.g. to meet the version requirements the client properties `nodePath` and `npxPath` can be used to specify the directory paths containing the node and npx executables. E.g.:
57-
58-
```matlab
59-
c.nodePath = '/home/username/.nvm/versions/node/v16.15.1/bin/'; % Optional, to use a non-system node install
60-
c.npxPath = '/home/username/.nvm/versions/node/v16.15.1/bin/';
61-
```
62-
63-
Setting the nodePath or npxPath values currently has no effect on Windows systems.
64-
The `checkDeps()` method on the client can be used to check if the client's dependencies are met, in which case it returns `true` and otherwise `false`. This method is called when the `build` method is called.
65-
6660
## Using an openapitools.json file
6761

68-
By default a configuration file, typically called called `openapitools.json`, is produced. This is populated by the builder and passed to the underlying tools. If command line invocation is used and if it does not already exist when the generator is invoked. A minimal generated configuration template is produced. Using a configuration file can be less error prone than the CLI based approach though they are equivalent. Storing the file in source control alongside code can be useful given the potential complexity of configurations. Furthermore it is easily overwritten accidentally and there are a great many potential configuration options that may be difficult to replicate if lost.
62+
By default a configuration file, called `openapitools.json`, is produced in the current directory. This is populated by the builder.
63+
An alternative name and path can be given by setting `outputConfigurationFile`.
64+
Using a configuration file can be less error prone than the CLI based approach though they are equivalent.
65+
Storing the file in source control alongside code can be useful given the potential complexity of configurations.
66+
Furthermore it is easily overwritten accidentally and there are a great many potential configuration options that may be difficult to replicate if lost.
6967

7068
The following is a sample `openapitools.json` file, it is typically located in the working directory being used by the generator.
7169
The schema for this file can provide further details: [https://github.com/OpenAPITools/openapi-generator-cli/blob/master/apps/generator-cli/src/config.schema.json](https://github.com/OpenAPITools/openapi-generator-cli/blob/master/apps/generator-cli/src/config.schema.json)
@@ -98,12 +96,14 @@ If an alternative file is preferred or if the configuration is not to be used:
9896

9997
```matlab
10098
% Property to indicate that an alternative json file should be used
101-
c.configurationFile = '/my/alternative/path/openapitools.json';
99+
c.inputConfigurationFile = '/my/alternative/path/openapitools.json';
102100
```
103101

102+
Set a non default output location:
103+
104104
```matlab
105105
% Property to indicate that an openapitools.json file should be used
106-
c.useFileConfiguration = false;
106+
c.outputConfigurationFile = false;
107107
```
108108

109109
## Package naming
@@ -123,12 +123,9 @@ c.copyrightNotice = "(c) 2023 My Company Name Inc.";
123123

124124
The value will be inserted as a comment in the header of generated `api` and `model` code files. As the client passes the value as an argument to `npx` certain string processing rules are applied:
125125

126-
* Parentheses, ( & ), will be escaped.
127-
* Spaces will be escaped.
128-
* Single and double quotes will be removed.
129-
130-
The copyright symbol "©" is supported. Hyphens are supported.
126+
* Single and double quotes and hyphens will be removed.
131127

128+
The copyright symbol "©" is supported.
132129
This method is appropriate for short, simple statements as shown in the example. If more extensive text or legal statements, e.g. licensing details, are required in the generated code, then careful modification of the `Software/Mustache/copyrightNotice.mustache` template file is the suggested approach. Comment symbols, `%`, should be prepended to any such text.
133130

134131
## Specifying properties
@@ -174,7 +171,7 @@ A log file is produced by the client containing generator output and other infor
174171

175172
## Generating a MATLAB client using the command line
176173

177-
The following commands show how a MATLAB client can be generated from a given spec. Using the provided generator without invoking MATLAB. Note that using the MATLAB builder can help to provide the initial syntax.
174+
The following commands show how a MATLAB client can be generated from a given spec. Using the provided generator without invoking MATLAB. Note that using the MATLAB builder can help to provide the initial syntax. Here `npx` is used to call a frontend to the underlying Java library.
178175

179176
```bash
180177
# Change to the packages software directory
@@ -189,7 +186,13 @@ If not working in the package's `Software` directory, use full paths and add the
189186
-t "<package_directory>/Software/Mustache" --additional-properties openapiRoot="<package_directory>/Software/MATLAB
190187
```
191188
192-
Note, the `-i` argument can also point to a local spec file in `.yaml` or `.json` format.
189+
```{note}
190+
The `-i` argument can also point to a local spec file in `.yaml` or `.json` format instead of a http(s) URL.
191+
````
192+
193+
```{hint}
194+
If the API specification is spread across multiple separate files, instead of specifying `-i` (which can only point to a single file), it is also possible to use `--input-spec-root-directory` instead. This can point to a whole directory with YAML and JSON files and this will then generate a single MATLAB client which covers all APIs and Models from all files combined.
195+
```
193196
194197
By default the client will be generated in a the current directory in a subdirectory named `OpenAPIClient`. This can be changed using the `-o` flag as shown in the syntax above. Similarly, by default the generator creates a package named `OpenAPIClient` which can be changed with the `--package-name` flag.
195198
@@ -199,8 +202,8 @@ An alternative Mustache files directory can be specified using the `-t` flag e.g
199202
200203
If not running from the `Software` directory the location of the `Software/MATLAB` directory must be provided through `--additional-properties=openapiRoot=<location-of-Software/MATLAB>`, e.g. `--additional-properties=openapiRoot=/work/openapi/Software/MATLAB`. Without this the generator will not be able to find some of the helper MATLAB files which need to be included in the generated package. (When using the MATLAB Builder this option is set automatically).
201204
202-
Finally it is possible to add another flag `-p AddOAuth=name` (where name can be chosen freely), this will then add authentication calls to *all* operations and generates skeleton [`requestAuth` and `getOAuthToken` code which can then be customized](CustomizingGeneratedCode.md#authentication) to add OAuth 2.0 authentication to *all* operations.
205+
See [Generator Configuration Options](./Options.md) for a complete overview of all, including a few more advanced, options.
203206
204-
Ultimately npx invokes Java to run the code having created the appropriate input arguments, one can avoid using npx and use Java directly, using npx initially to get the basic form of the Java command and then adjusting this can be useful.
207+
Ultimately npx invokes Java to run the code having created the appropriate input arguments, one can avoid using Node.js package and use Java directly, using Node.js or the MATLAB client initially to get the basic form of the Java command and then adjusting this can be useful.
205208
206-
[//]: # (Copyright 2020-2023 The MathWorks, Inc.)
209+
[//]: # (Copyright 2020-2025 The MathWorks, Inc.)

Documentation/CustomizingGeneratedCode.md

+27-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
# Customizing Generated Code
22

3-
After the MATLAB code has been generated it may be necessary to customize the generated code. For example, the package only supports `application/json` and `application/x-www-form-urlencoded` body parameter inputs and `application/json` body replies. If some operations in the API only support other types e.g. `application/xml` the generated code will have to be manually modified to make these calls possible. See the [Unsupported Content-Type](#unsupported-content-type) section below.
3+
After the MATLAB code has been generated it may be necessary to customize the generated code. The following describes tips to help in doing so and typical points within the generated code where modifications might be made.
44

5-
Also, when it comes to authentication, Basic or Digest authentication should work by default (as long as the correct `matlab.net.http.Credentials` are provided to the client object). API Key based authentication should work as well (if the correct `apiKey` is set on the client object). However, *proper* oAuth authentication may need customization. See the [Authentication](#authentication) section below to learn more about how the generated classes work with authentication and what customizations may be needed.
5+
## Hints
66

7-
Lastly, the `BaseClient` is generated with `preSend` and `postSend` methods which will be called by all operations right before and after sending their requests. By default these methods do not actually do anything but they can be [customized](#presend-and-postsend-methods).
7+
```{hint}
8+
Generated models derive from a `JSONMapper` base class and the properties in the Model classes are "annotated" using `JSONMapper` "annotations". See [References/JSONMapper](JSONMapper.md) to learn more about this class.
9+
```
10+
11+
```{hint}
12+
For common functionality consider changing the `BaseClient` to that changes propagate to all derived API classes.
13+
```
814

915
```{hint}
10-
Generated models derived from a `JSONMapper` base class and the properties in the Model classes are "annotated" using `JSONMapper` "annotations". See [References/JSONMapper](JSONMapper.md) to learn more about this class.
16+
If changing the `preSend`, `postSend` or `BaseClient` call a function perhaps in separate namespace rather than several lines of code, [https://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html](https://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html) so that added code is more distinct from the generated code and reinserting just one line of code adds back functionality making diffs or merges simpler.
1117
```
1218

19+
## Source control
20+
21+
Before modifying the code it is strongly advised to use source control of some form so if the code is regenerated later changes are not lost and changes can be checked against generated code.
22+
Source controlling the source spec and any build scripts is also recommended so that changes to them can be recorded over time also. This provides an exact means of recording what version of a spec generated what version of a client and potentially recreating it if necessary. This package notes the version of the package used to generate code as a comment in the code itself.
23+
24+
## Selective generation
25+
26+
The generator takes standard openapi-tools arguments that would allow generation of the APIs or the Models or subsets. This is mostly useful if editing the spec itself or if it is known that a only specific part of a spec has changed. One can use a `.openapi-generator-ignore` file to define files to not regenerate.
27+
1328
## Unsupported Content-Type
1429

30+
The package only supports `application/json` and `application/x-www-form-urlencoded` body parameter inputs and `application/json` body replies. If some operations in the API only support other types e.g. `application/xml` the generated code will have to be manually modified to make these calls possible.
31+
1532
Each and every method/operation will check the in- and output content-types if there are any. For example the `addPet` method for the Pet Store v3 example will contain code like the following:
1633

1734
```matlab
@@ -38,6 +55,8 @@ When manually adding support for other content-types for specific operations, ma
3855

3956
In OpenAPI 3, the spec can declare various named authentication methods. And then, for each separate operation, it can specify whether it requires authentication at all, and if so, which of the named methods are supported.
4057

58+
Basic or Digest authentication should work by default (as long as the correct `matlab.net.http.Credentials` are provided to the client object). API Key based authentication should work as well (if the correct `apiKey` is set on the client object). However, *proper* oAuth authentication may need customization. See the [Authentication](#authentication) section below to learn more about how the generated classes work with authentication and what customizations may be needed.
59+
4160
For the *entire* package exactly *one* `requestAuth` method is generated and it is part of the generated `<PackageName>.BaseClient` class. All the API classes derive from this `BaseClient` class. The `requestAuth` method will contain a `switch, case, otherwise` statement to handle the various authentication mechanisms based on their name. For example for the Pet Store v3 example, it will look like the following:
4261

4362
```matlab
@@ -65,7 +84,7 @@ end
6584
Each operation which requires authentication will call this method before making its actual request. The `requestAuth` method then updates the request or HTTPOptions with the relevant settings and returns the updated request/options as output.
6685

6786
```{hint}
68-
Not all specs follow this approach strictly. For example, in some cases where *all* operations require the same authentication, some specs may not list the supported authentication methods on a per operation basis. In that case, the generator will *not* generate calls to `requestAuth` in the operation methods. In such cases, consider using [`preSend`](#presend) to authenticate the requests before they are made.
87+
Not all specs follow this approach strictly. For example, in some cases where *all* operations require the same authentication, some specs may not list the supported authentication methods on a per operation basis. In that case, the generator will *not* generate calls to `requestAuth` in the operation methods. In such cases, consider using the [`AddOAuth` option](./Options.md#addoauth) when generating the client or use [`preSend`](#presend) to authenticate the requests before they are made.
6988
```
7089

7190
```{note}
@@ -124,7 +143,7 @@ As indicated in the comments of this code, this is really just template code whi
124143

125144
## preSend and postSend methods
126145

127-
The `BaseClient` class is generated with the following two methods:
146+
The `BaseClient` is generated with `preSend` and `postSend` methods which will be called by all operations right before and after sending their requests. By default these methods do not actually do anything but they can be [customized](#presend-and-postsend-methods). The `BaseClient` class `preSend` and `postSend` methods:
128147

129148
```matlab
130149
function [request, httpOptions, uri] = preSend(obj, operationId, request, httpOptions, uri) %#ok<INUSL>
@@ -163,7 +182,7 @@ By default the generated `preSend` method does not do anything but it offers an
163182

164183
The `preSend` method can be customized if *all or at least most* requests need some customization before being send. For example, a header field could be added.
165184

166-
Further, some APIs require authentication on *all* operations and then do not specify on a *per operation* basis that they require authentication. In that case it is possible to add the authentication in `preSend`, note however that it is also possible to use the [`-p AddOAuth=name` option](BuildClient.md#generating-a-matlab-client-using-the-command-line) for this.
185+
Further, some APIs require authentication on *all* operations and then do not specify on a *per operation* basis that they require authentication. In that case it is possible to add the authentication in `preSend`, note however that it is also possible to use the [`AddOAuth` option](./Options.md#addoauth) for this.
167186

168187
```{note}
169188
If only a few specific operations need customization it makes more sense to edit their methods in the API classes directly rather than handling this in `preSend`.
@@ -187,4 +206,4 @@ client.globalProperty = "models=""User:Pet""" % Generate the User and Pet m
187206
client.globalProperty = "skipFormModel = false" % Generate for OAS3 and ver < v5.x using the form parameters in "requestBody"
188207
```
189208

190-
[//]: # (Copyright 2023 The MathWorks, Inc.)
209+
[//]: # (Copyright 2023-2024 The MathWorks, Inc.)

0 commit comments

Comments
 (0)