Skip to content
Merged
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
1 change: 1 addition & 0 deletions docs/generators/typescript-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|useSquareBracketsInArrayNames|Setting this property to true will add brackets to array attribute names, e.g. my_values[].| |false|
|validationAttributes|Setting this property to true will generate the validation attributes of model properties.| |false|
|withInterfaces|Setting this property to true will generate interfaces next to the default class implementations.| |false|
|withRequestOptsInInterface|Setting this property to true will include *RequestOpts methods in the API interface declarations. Set to false to keep them only on the class.| |true|
|withoutRuntimeChecks|Setting this property to true will remove any runtime checks on the request and response payloads. Payloads will be casted to their expected types.| |false|

## IMPORT MAPPING
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ public class TypeScriptFetchClientCodegen extends AbstractTypeScriptClientCodege
public static final String PASCAL_CASE = "PascalCase";
public static final String USE_SQUARE_BRACKETS_IN_ARRAY_NAMES = "useSquareBracketsInArrayNames";
public static final String VALIDATION_ATTRIBUTES = "validationAttributes";
public static final String WITH_REQUEST_OPTS_IN_INTERFACE = "withRequestOptsInInterface";

@Getter @Setter
protected String npmRepository = null;
@Getter @Setter
protected String importFileExtension = "";
private boolean useSingleRequestParameter = true;
private boolean prefixParameterInterfaces = false;
private boolean withRequestOptsInInterface = true;
protected boolean addedApiIndex = false;
protected boolean addedModelIndex = false;
protected boolean withoutRuntimeChecks = false;
Expand Down Expand Up @@ -130,6 +132,7 @@ public TypeScriptFetchClientCodegen() {
this.cliOptions.add(new CliOption(FILE_NAMING, "Naming convention for the output files: 'PascalCase', 'camelCase', 'kebab-case'.").defaultValue(this.fileNaming));
this.cliOptions.add(new CliOption(USE_SQUARE_BRACKETS_IN_ARRAY_NAMES, "Setting this property to true will add brackets to array attribute names, e.g. my_values[].", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(VALIDATION_ATTRIBUTES, "Setting this property to true will generate the validation attributes of model properties.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.FALSE.toString()));
this.cliOptions.add(new CliOption(WITH_REQUEST_OPTS_IN_INTERFACE, "Setting this property to true will include *RequestOpts methods in the API interface declarations. Set to false to keep them only on the class.", SchemaTypeUtil.BOOLEAN_TYPE).defaultValue(Boolean.TRUE.toString()));
}

@Override
Expand Down Expand Up @@ -273,6 +276,11 @@ public void processOpts() {
}
writePropertyBack(PREFIX_PARAMETER_INTERFACES, getPrefixParameterInterfaces());

if (additionalProperties.containsKey(WITH_REQUEST_OPTS_IN_INTERFACE)) {
this.setWithRequestOptsInInterface(convertPropertyToBoolean(WITH_REQUEST_OPTS_IN_INTERFACE));
}
writePropertyBack(WITH_REQUEST_OPTS_IN_INTERFACE, getWithRequestOptsInInterface());

if (additionalProperties.containsKey(NPM_NAME)) {
addNpmPackageGeneration();
}
Expand Down Expand Up @@ -1084,6 +1092,14 @@ private void setPrefixParameterInterfaces(boolean prefixParameterInterfaces) {
this.prefixParameterInterfaces = prefixParameterInterfaces;
}

private boolean getWithRequestOptsInInterface() {
return withRequestOptsInInterface;
}

private void setWithRequestOptsInInterface(boolean withRequestOptsInInterface) {
this.withRequestOptsInInterface = withRequestOptsInInterface;
}

private static boolean itemsAreUniqueId(CodegenProperty items) {
if (items != null && items.items != null) {
return itemsAreUniqueId(items.items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterIn
*/
export interface {{classname}}Interface {
{{#operation}}
{{#withRequestOptsInInterface}}
/**
* Creates request options for {{nickname}} without sending the request
{{#allParams}}
Expand All @@ -55,6 +56,7 @@ export interface {{classname}}Interface {
*/
{{nickname}}RequestOpts({{#allParams.0}}requestParameters: {{#prefixParameterInterfaces}}{{classname}}{{/prefixParameterInterfaces}}{{operationIdCamelCase}}Request{{/allParams.0}}): Promise<runtime.RequestOpts>;

{{/withRequestOptsInInterface}}
/**
* {{&notes}}
{{#summary}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public Map<String, String> createOptions() {
.put(TypeScriptFetchClientCodegen.STRING_ENUMS, STRING_ENUMS)
.put(TypeScriptFetchClientCodegen.USE_SQUARE_BRACKETS_IN_ARRAY_NAMES, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.VALIDATION_ATTRIBUTES, Boolean.FALSE.toString())
.put(TypeScriptFetchClientCodegen.WITH_REQUEST_OPTS_IN_INTERFACE, Boolean.TRUE.toString())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -458,6 +459,55 @@ public void testValidationAttributesWithWithoutRuntimeChecks() throws IOExceptio
TestUtils.assertFileContains(modelsIndex, "[property: string]:");
}

@Test(description = "Verify withRequestOptsInInterface=true (default) includes RequestOpts in interface")
public void testRequestOptsInInterfaceByDefault() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put(TypeScriptFetchClientCodegen.WITH_INTERFACES, true);

File output = generate(properties);

Path apiFile = Paths.get(output + "/apis/PetControllerApi.ts");
TestUtils.assertFileExists(apiFile);

// Read file content and split into interface and class sections
String content = new String(Files.readAllBytes(apiFile), StandardCharsets.UTF_8);
int interfaceStart = content.indexOf("export interface PetControllerApiInterface");
int classStart = content.indexOf("export class PetControllerApi");
String interfaceSection = content.substring(interfaceStart, classStart);

// Interface should contain RequestOpts methods
assertThat(interfaceSection).contains("addPetRequestOpts(");

// Class should also contain RequestOpts methods
String classSection = content.substring(classStart);
assertThat(classSection).contains("async addPetRequestOpts(");
}

@Test(description = "Verify withRequestOptsInInterface=false excludes RequestOpts from interface but keeps them on class")
public void testRequestOptsNotInInterfaceWhenDisabled() throws IOException {
Map<String, Object> properties = new HashMap<>();
properties.put(TypeScriptFetchClientCodegen.WITH_INTERFACES, true);
properties.put(TypeScriptFetchClientCodegen.WITH_REQUEST_OPTS_IN_INTERFACE, false);

File output = generate(properties);

Path apiFile = Paths.get(output + "/apis/PetControllerApi.ts");
TestUtils.assertFileExists(apiFile);

// Read file content and split into interface and class sections
String content = new String(Files.readAllBytes(apiFile), StandardCharsets.UTF_8);
int interfaceStart = content.indexOf("export interface PetControllerApiInterface");
int classStart = content.indexOf("export class PetControllerApi");
String interfaceSection = content.substring(interfaceStart, classStart);

// Interface should NOT contain RequestOpts methods
assertThat(interfaceSection).doesNotContain("RequestOpts");

// Class should still contain RequestOpts methods
String classSection = content.substring(classStart);
assertThat(classSection).contains("async addPetRequestOpts(");
}

private static File generate(
Map<String, Object> properties
) throws IOException {
Expand Down
Loading