Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/showcase/src/style/dark-theme/dark-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
--radius-5: 5px;
--radius-10: 10px;
--radius-25: 25px;
--spacing-0: 0px;
--spacing-0: 0;
Comment thread
kpanot marked this conversation as resolved.
--spacing-1: 1px;
--spacing-5: 5px;
--spacing-10: 10px;
Expand Down
2 changes: 1 addition & 1 deletion apps/showcase/src/style/horizon-theme/horizon-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
--radius-5: 5px;
--radius-10: 10px;
--radius-25: 25px;
--spacing-0: 0px;
--spacing-0: 0;
--spacing-1: 1px;
--spacing-5: 5px;
--spacing-10: 10px;
Expand Down
2 changes: 1 addition & 1 deletion apps/showcase/src/style/theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
--radius-5: 5px;
--radius-10: 10px;
--radius-25: 25px;
--spacing-0: 0px;
--spacing-0: 0;
--spacing-1: 1px;
--spacing-5: 5px;
--spacing-10: 10px;
Expand Down
29 changes: 19 additions & 10 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,14 @@
"source",
"specs"
],
"dependsOn": [{
"projects": ["test-helpers"],
"target": "build"
}],
"dependsOn": [
{
"projects": [
"test-helpers"
],
"target": "build"
}
],
"outputs": [
"{projectRoot}/dist-test/junit.xml",
"{projectRoot}/coverage/cobertura-coverage.xml"
Expand All @@ -247,10 +251,14 @@
},
"test-int": {
"executor": "@nx/jest:jest",
"dependsOn": [{
"projects": ["test-helpers"],
"target": "build"
}],
"dependsOn": [
{
"projects": [
"test-helpers"
],
"target": "build"
}
],
"inputs": [
{
"env": "RUNNER_OS"
Expand Down Expand Up @@ -732,5 +740,6 @@
"appsDir": "apps"
},
"cacheDirectory": ".cache/nx",
"nxCloudId": "63e6951e043da20dd8321aec"
}
"nxCloudId": "63e6951e043da20dd8321aec",
"analytics": false
}
2 changes: 1 addition & 1 deletion packages/@ama-openapi/create/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"typescript-eslint": "~8.57.0"
},
"generatorDependencies": {
"@redocly/openapi-core": "~2.24.0"
"@redocly/openapi-core": "~2.25.0"
},
"engines": {
"node": "^22.17.0 || ^24.0.0",
Expand Down
20 changes: 20 additions & 0 deletions packages/@ama-sdk/schematics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,26 @@ The Body request parameter generated in the SDK can also be defined in the speci
> [!WARNING]
> This vendor extension will take precedence over the global property `requestBodyTransform`.

##### Dynamic Enums Type

By default, enums are generated as static types. You can enable dynamic enum type generation by using the global property option `useDynamicEnumType` by adding `--global-property useDynamicEnumType=true` to the generator command.

This option allows enums to be generated as dynamic types that can handle additional values not explicitly defined in the specification, making your SDK more resilient to API changes.

Example:

```shell
yarn schematics @ama-sdk/schematics:typescript-core --spec-path ./swagger-spec.yaml --global-property useDynamicEnumType=true
```

##### Override Discriminator Type with Value

By default, when a discriminator is used in the specification file, the children models are generated with a type that corresponds to the discriminator property and its value (for example, if the discriminator is `petType` and its value is `Dog`, the generated type for the children model will be `Dog`). You can enable the generation of the discriminator property as a type instead of its value by using the global property option `overrideDiscriminatorTypeWithValue` by adding `--global-property overrideDiscriminatorTypeWithValue=true` to the generator command.

##### Children Model List Type Suffix

The options `childrenModelListTypeSuffix` will add a suffix to the generated type of the list of children models when a discriminator is used in the specification file. If not specified the list of object children will not be generated.

##### Extensible models

You may be in a case in which you want to be able to extend your SDK models and therefore ensure that revivers are generated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public abstract class AbstractTypeScriptClientCodegen extends DefaultCodegen imp
private final boolean stringifyDate;
private final boolean allowModelExtension;
private final boolean useLegacyDateExtension;
private final boolean useDynamicEnumType;
private final boolean overrideDiscriminatorTypeWithValue;
private final String requestBodyTransform;
private final String childrenModelListTypeSuffix;

public AbstractTypeScriptClientCodegen() {
super();
Expand Down Expand Up @@ -120,6 +123,12 @@ public AbstractTypeScriptClientCodegen() {
stringifyDate = stringifyDateString != null ? !"false".equalsIgnoreCase(stringifyDateString) : true;
String requestBodyTransformString = GlobalSettings.getProperty("requestBodyTransform");
requestBodyTransform = requestBodyTransformString != null ? requestBodyTransformString : "";
String useDynamicEnumTypeString = GlobalSettings.getProperty("useDynamicEnumType");
useDynamicEnumType = useDynamicEnumTypeString != null ? !"false".equalsIgnoreCase(useDynamicEnumTypeString) : false;
String overrideDiscriminatorTypeWithValueString = GlobalSettings.getProperty("overrideDiscriminatorTypeWithValue");
overrideDiscriminatorTypeWithValue = overrideDiscriminatorTypeWithValueString != null ? !"false".equalsIgnoreCase(overrideDiscriminatorTypeWithValueString) : false;
String childrenModelListTypeSuffixString = GlobalSettings.getProperty("childrenModelListTypeSuffix");
childrenModelListTypeSuffix = childrenModelListTypeSuffixString;
typeMapping.put("DateTime", useLegacyDateExtension ? "utils.DateTime" : getDateTimeStandardTime(stringifyDate));
typeMapping.put("Date", useLegacyDateExtension ? "utils.Date" : getDateType(stringifyDate));
//TODO binary should be mapped to byte array
Expand Down Expand Up @@ -161,6 +170,7 @@ public AbstractTypeScriptClientCodegen() {
additionalProperties.put("resourceFromPath", new LambdaHelper.ResourceFromPath());
additionalProperties.put("areaFromPath", new LambdaHelper.AreaFromPath());
additionalProperties.put("noEmptyLines", new LambdaHelper.RemoveEmptyLines());
additionalProperties.put("noMultipleEmptyLines", new LambdaHelper.RemoveDuplicateEmptyLines());
additionalProperties.put("replaceWithEmptyExportIfNeeded", new LambdaHelper.ReplaceWithTextIfEmpty("export {};"));
additionalProperties.put("propertyDeclaration", new LambdaHelper.PropertyDeclaration());
additionalProperties.put("propertyAccess", new LambdaHelper.PropertyAccess());
Expand Down Expand Up @@ -657,6 +667,15 @@ public ModelsMap postProcessModels(ModelsMap objs) {
// Set additional properties and check non model objects
for (ModelMap modelMap : models) {
CodegenModel model = modelMap.getModel();

if (model.discriminator != null) {
List<String> discriminatorValues = new ArrayList<String>();
for (CodegenDiscriminator.MappedModel mm : model.discriminator.getMappedModels()) {
discriminatorValues.add(mm.getModelName());
}
model.vendorExtensions.put("x-discriminator-mapping-values", discriminatorValues);
}

if (model.isEnum) {
nonObjectModels.add(toModelName(model.name));
}
Expand Down Expand Up @@ -744,6 +763,9 @@ private ModelsMap postProcessImports(ModelsMap objs) {
@Override
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
System.out.println("Non object models: " + nonObjectModels.toString());

Map<String, String> discriminatorField = new HashMap<>();
Map<String, String> discriminatorValue = new HashMap<>();
for (Map.Entry<String, ModelsMap> entry : objs.entrySet()) {
ModelsMap modelsMap = entry.getValue();
List<ModelMap> modelMaps = modelsMap.getModels();
Expand All @@ -769,12 +791,43 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
prop.vendorExtensions.put("nonObjectDefinition", nonObjectDefinition);
}
}

if (model.discriminator != null) {
for (CodegenDiscriminator.MappedModel mm : model.discriminator.getMappedModels()) {
discriminatorField.put(mm.getModelName(), model.discriminator.getPropertyName());
discriminatorValue.put(mm.getModelName(), mm.getMappingName());
}
}
}
}

objs = super.postProcessAllModels(objs);
for (Map.Entry<String, ModelsMap> entry : objs.entrySet()) {
entry.setValue(this.postProcessImports(entry.getValue()));
ModelsMap modelsMap = entry.getValue();
List<ModelMap> modelMaps = modelsMap.getModels();
for (ModelMap _modelMap : modelMaps) {
CodegenModel model = _modelMap.getModel();
// Set the useDynamicEnumType vendor extension for the model and its properties to be able to use it in the templates
model.vendorExtensions.put("x-use-dynamic-enum-type", useDynamicEnumType);
// Set the overrideDiscriminatorTypeWithValue vendor extension for the model to be able to use it in the templates
model.vendorExtensions.put("x-override-discriminator-type-with-value", overrideDiscriminatorTypeWithValue);
// Set the childrenModelListTypeSuffix vendor extension for the model to be able to use it in the templates
model.vendorExtensions.put("x-children-model-list-type-suffix", childrenModelListTypeSuffix);
for (CodegenProperty prop : model.getAllVars()) {
if (prop.isEnum) {
prop.vendorExtensions.put("x-use-dynamic-enum-type", useDynamicEnumType);
}
}

// Set discriminator field and value as vendor extension for the model if any, to be able to use it in the templates
if (discriminatorField.containsKey(model.classname) && discriminatorValue.containsKey(model.classname)) {
model.vendorExtensions.put("x-discriminator-field", discriminatorField.get(model.classname));
model.vendorExtensions.put("x-discriminator-value", discriminatorValue.get(model.classname));
}
}
}

// if allowModelExtension is true, we don't need to analyze the conditions since we want to ensure the generation of the revivers
if (!allowModelExtension) {
Predicate<CodegenProperty> varRequiredReviverPredicate = var -> !(var.isPrimitiveType || var.complexType != null || var.isEnum || var.isEnumRef);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,4 +591,22 @@ public String formatFragment(String fragment) {
return requestBodyTransform.replace("{{bodyRequest}}", fragment);
}
}

public static class RemoveDuplicateEmptyLines extends CustomLambda {

final String whitespaceTransform = "(\\n\\r?){3,}";
final String trailingWhitespaceTransform = "(\\n\\r?){2,}$";

public RemoveDuplicateEmptyLines() {
}

@Override
public String formatFragment(String fragment) {
if (fragment == null || fragment.equals("")) {
return fragment;
}
fragment = fragment.replaceAll(whitespaceTransform, "\n");
return fragment.replaceAll(trailingWhitespaceTransform, "\n");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{#noUnusedImport}}
{{#imports}}
import { {{import}} } from '../../models/base/{{#kebabCase}}{{import}}{{/kebabCase}}/index';
import type { {{import}} } from '../../models/base/{{#kebabCase}}{{import}}{{/kebabCase}}/index';
{{#keepRevivers}}
import { revive{{import}} } from '../../models/base/{{#kebabCase}}{{import}}{{/kebabCase}}/{{#kebabCase}}{{import}}{{/kebabCase}}.reviver';
{{/keepRevivers}}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{{#noMultipleEmptyLines}}
{{#noUnusedImport}}
/**
* Model: {{classname}}
Expand All @@ -15,7 +16,7 @@ import {utils} from '@ama-sdk/core';
{{/models}}

{{#imports}}
import { {{import}} } from '../{{#kebabCase}}{{import}}{{/kebabCase}}';
import type { {{import}} } from '../{{#kebabCase}}{{import}}{{/kebabCase}}';
{{/imports}}

{{#models}}
Expand Down Expand Up @@ -69,6 +70,10 @@ export interface {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}{
{{/x-map-name}}
{{/vendorExtensions}}
{{/vars}}
{{#parent}}{{#vendorExtensions}}{{#x-discriminator-field}}{{#x-override-discriminator-type-with-value}}
/** @see {{parent}}.{{x-discriminator-field}} */
{{x-discriminator-field}}: '{{x-discriminator-value}}';
{{/x-override-discriminator-type-with-value}}{{/x-discriminator-field}}{{/vendorExtensions}}{{/parent}}
}
{{/oneOf}}
{{#oneOf}}
Expand All @@ -84,7 +89,11 @@ export type {{classname}} = {{#oneOf}}{{.}}{{^-last}} | {{/-last}}{{/oneOf}};
export const {{#upperSnakeCase}}list{{#removeBrackets}}{{{datatypeWithEnum}}}{{/removeBrackets}}{{/upperSnakeCase}} = [{{#trimComma}}{{#vendorExtensions}}{{#x-sanitized-allowable-values}}'{{.}}'{{^-last}}, {{/-last}}{{/x-sanitized-allowable-values}}{{/vendorExtensions}}{{/trimComma}}] as const;

/** List of available values for {{#removeBrackets}}{{{datatypeWithEnum}}}{{/removeBrackets}} */
{{#vendorExtensions}}{{#x-use-dynamic-enum-type}}
export type {{#removeBrackets}}{{{datatypeWithEnum}}}{{/removeBrackets}} = typeof {{#upperSnakeCase}}list{{#removeBrackets}}{{{datatypeWithEnum}}}{{/removeBrackets}}{{/upperSnakeCase}}[number];
{{/x-use-dynamic-enum-type}}{{^x-use-dynamic-enum-type}}
export type {{#removeBrackets}}{{{datatypeWithEnum}}}{{/removeBrackets}} = {{#trimPipe}}{{#allowableValues}}{{#values}}'{{.}}' | {{/values}}{{/allowableValues}}{{/trimPipe}};
{{/x-use-dynamic-enum-type}}{{/vendorExtensions}}
{{/isEnum}}
{{/allVars}}
{{/hasEnums}}
Expand All @@ -97,12 +106,34 @@ export const {{baseName}}Pattern = {{#parseRegexp}}{{{pattern}}}{{/parseRegexp}}
{{/vars}}
{{/isEnum}}
{{#isEnum}}
/** Array of {{classname}} items */
export const {{#upperSnakeCase}}list{{classname}}{{/upperSnakeCase}} = [{{#trimComma}}{{#allowableValues}}{{#values}}'{{.}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}{{/trimComma}}] as const;
/** @inheritDoc */
export const {{#upperSnakeCase}}list{{classname}}{{/upperSnakeCase}} = [{{#trimComma}}{{#allowableValues}}{{#values}}'{{.}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}{{/trimComma}}] as const satisfies {{classname}}[];

/** List of available values for {{classname}} */
export type {{classname}} = {{#trimPipe}}{{#allowableValues}}{{#values}}'{{.}}' | {{/values}}{{/allowableValues}}{{/trimPipe}};
{{#vendorExtensions}}{{#x-use-dynamic-enum-type}}
export type {{classname}} = typeof {{#upperSnakeCase}}list{{classname}}{{/upperSnakeCase}}[number];
{{/x-use-dynamic-enum-type}}{{^x-use-dynamic-enum-type}}
export type {{classname}} = {{#trimPipe}}{{#allowableValues}}{{#values}}'{{.}}'{{^-last}} | {{/-last}}{{/values}}{{/allowableValues}}{{/trimPipe}};
{{/x-use-dynamic-enum-type}}{{/vendorExtensions}}
{{/isEnum}}

{{#vendorExtensions}}{{#discriminator}}{{#x-children-model-list-type-suffix}}
/** Mapping between discriminator values and their corresponding types for {{classname}} */
export type {{classname}}{{x-children-model-list-type-suffix}} = {{#x-discriminator-mapping-values}}{{.}}{{^-last}} | {{/-last}}{{/x-discriminator-mapping-values}};
{{/x-children-model-list-type-suffix}}{{/discriminator}}{{/vendorExtensions}}

{{#parent}}{{#vendorExtensions}}{{#x-discriminator-field}}
/**
* Type guard for {{classname}} implementing the discriminator logic
* @param data The data to check
* @returns True if the data is of type {{classname}}, false otherwise
*/
export const is{{classname}} = (data: {{parent}}): data is {{classname}} => {
return data['{{x-discriminator-field}}'] === '{{x-discriminator-value}}';
};
{{/x-discriminator-field}}{{/vendorExtensions}}{{/parent}}

{{/model}}
{{/models}}
{{/noUnusedImport}}
{{/noMultipleEmptyLines}}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"storageDir": ".openapi-generator",
"generators": {
"<%=projectName%>-<%=projectPackageName%>": {
"useDynamicEnumType": true,
"generatorName": "typescriptFetch",
"output": ".",
"inputSpec": ""
Expand Down
2 changes: 1 addition & 1 deletion packages/@ama-styling/figma-extractor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"bin": "./cli/figma-extract.js",
"generatorDependencies": {
"@ama-styling/style-dictionary": "workspace:~",
"style-dictionary": "~5.3.0"
"style-dictionary": "~5.4.0"
},
"peerDependencies": {
"@ama-sdk/client-fetch": "workspace:~",
Expand Down
46 changes: 46 additions & 0 deletions packages/@o3r-training/showcase-sdk/open-api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ tags:
- name: user
description: Operations about user
paths:
/animal:
get:
tags:
- "dummy"
summary: Animal get
description: Get an animal
operationId: animalGet
responses:
200:
description: "Successful operation"
content:
application/json:
schema:
$ref: '#/components/schemas/Animal'
/pet:
post:
servers:
Expand Down Expand Up @@ -630,6 +644,38 @@ externalDocs:
url: 'http://swagger.io'
components:
schemas:
Animal:
type: object
required:
- petType
properties:
petType:
type: string
enum: [cat, dog] # <- optional enum, can be forgotten in the declaration without changing generated code
discriminator:
propertyName: petType
mapping: # <- mandatory
dog: "#/components/schemas/Dog"
cat: "#/components/schemas/Cat"
Dog:
allOf:
- $ref: '#/components/schemas/Animal'
- type: object
properties:
bark:
type: boolean
breed:
type: string
enum: [Dingo, Husky, Retriever, Shepherd]
Cat:
allOf:
- $ref: '#/components/schemas/Animal'
- type: object
properties:
hunts:
type: boolean
age:
type: integer
Order:
x-swagger-router-model: io.swagger.petstore.model.Order
properties:
Expand Down
Loading
Loading