Skip to content

Commit

Permalink
Revert "feat(java): revert public constructors in sdk and model (#4554)"
Browse files Browse the repository at this point in the history
This reverts commit 16053fc.
  • Loading branch information
dcb6 authored Sep 5, 2024
1 parent 16053fc commit eceb8bd
Show file tree
Hide file tree
Showing 94 changed files with 5,935 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ default JsonInclude jsonInclude() {
return JsonInclude.NON_ABSENT;
}

@Value.Default
@JsonProperty("enable-public-constructors")
default Boolean enablePublicConstructors() {
return false;
}

enum JsonInclude {
NON_EMPTY("non-empty"),
NON_ABSENT("non-absent");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.fern.java.generators;

import static java.util.TimeZone.LONG;

import com.fasterxml.jackson.annotation.JsonValue;
import com.fern.ir.model.types.AliasTypeDeclaration;
import com.fern.ir.model.types.PrimitiveType;
Expand All @@ -25,16 +23,13 @@ public final class AliasGenerator extends AbstractFileGenerator {
private static final String VALUE_FIELD_NAME = "value";
private static final String OF_METHOD_NAME = "of";
private final AliasTypeDeclaration aliasTypeDeclaration;
private final boolean publicConstructorsEnabled;

public AliasGenerator(
ClassName className,
AbstractGeneratorContext<?, ?> generatorContext,
AliasTypeDeclaration aliasTypeDeclaration,
boolean publicConstructorsEnabled) {
AliasTypeDeclaration aliasTypeDeclaration) {
super(className, generatorContext);
this.aliasTypeDeclaration = aliasTypeDeclaration;
this.publicConstructorsEnabled = publicConstructorsEnabled;
}

@Override
Expand Down Expand Up @@ -71,7 +66,10 @@ public GeneratedJavaFile generateFile() {

private MethodSpec getConstructor(TypeName aliasTypeName) {
return MethodSpec.constructorBuilder()
.addModifiers(publicConstructorsEnabled ? Modifier.PUBLIC : Modifier.PRIVATE)
.addModifiers(
this.generatorContext.getCustomConfig().enablePublicConstructors()
? Modifier.PUBLIC
: Modifier.PRIVATE)
.addParameter(aliasTypeName, VALUE_FIELD_NAME)
.addStatement("this.value = value")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,20 @@ public final class ObjectGenerator extends AbstractFileGenerator {
private final Optional<GeneratedJavaInterface> selfInterface;
private final Map<TypeId, GeneratedJavaInterface> allGeneratedInterfaces;
private final List<GeneratedJavaInterface> extendedInterfaces = new ArrayList<>();
private final boolean publicConstructorsEnabled;

public ObjectGenerator(
ObjectTypeDeclaration objectTypeDeclaration,
Optional<GeneratedJavaInterface> selfInterface,
List<GeneratedJavaInterface> extendedInterfaces,
AbstractGeneratorContext<?, ?> generatorContext,
Map<TypeId, GeneratedJavaInterface> allGeneratedInterfaces,
ClassName className,
boolean publicConstructorsEnabled) {
ClassName className) {
super(className, generatorContext);
this.objectTypeDeclaration = objectTypeDeclaration;
this.selfInterface = selfInterface;
selfInterface.ifPresent(this.extendedInterfaces::add);
this.extendedInterfaces.addAll(extendedInterfaces);
this.allGeneratedInterfaces = allGeneratedInterfaces;
this.publicConstructorsEnabled = publicConstructorsEnabled;
}

@Override
Expand Down Expand Up @@ -117,7 +114,7 @@ public GeneratedObject generateFile() {
enrichedObjectProperties,
implementsInterfaces,
true,
publicConstructorsEnabled,
generatorContext.getCustomConfig().enablePublicConstructors(),
generatorContext.deserializeWithAdditionalProperties(),
generatorContext.getCustomConfig().jsonInclude());
TypeSpec objectTypeSpec = genericObjectGenerator.generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,24 @@ public final class SingleTypeGenerator implements Type.Visitor<Optional<Generate
private final ClassName className;
private final Map<TypeId, GeneratedJavaInterface> allGeneratedInterfaces;
private final boolean fromErrorDeclaration;
private final boolean publicConstructorsEnabled;

public SingleTypeGenerator(
AbstractGeneratorContext<?, ?> generatorContext,
DeclaredTypeName declaredTypeName,
ClassName className,
Map<TypeId, GeneratedJavaInterface> allGeneratedInterfaces,
boolean fromErrorDeclaration,
boolean publicConstructorsEnabled) {
boolean fromErrorDeclaration) {
this.generatorContext = generatorContext;
this.className = className;
this.allGeneratedInterfaces = allGeneratedInterfaces;
this.declaredTypeName = declaredTypeName;
this.fromErrorDeclaration = fromErrorDeclaration;
this.publicConstructorsEnabled = publicConstructorsEnabled;
}

@Override
public Optional<GeneratedJavaFile> visitAlias(AliasTypeDeclaration value) {
if (generatorContext.getCustomConfig().wrappedAliases() || fromErrorDeclaration) {
AliasGenerator aliasGenerator =
new AliasGenerator(className, generatorContext, value, publicConstructorsEnabled);
AliasGenerator aliasGenerator = new AliasGenerator(className, generatorContext, value);
return Optional.of(aliasGenerator.generateFile());
}
return Optional.empty();
Expand All @@ -70,8 +66,7 @@ public Optional<GeneratedJavaFile> visitObject(ObjectTypeDeclaration value) {
extendedInterfaces,
generatorContext,
allGeneratedInterfaces,
className,
publicConstructorsEnabled);
className);
GeneratedObject generatedObject = objectGenerator.generateFile();
return Optional.of(GeneratedJavaFile.builder()
.className(generatedObject.getClassName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,11 @@ public final class TypesGenerator {
private final Map<TypeId, TypeDeclaration> typeDeclarations;
private final Map<ErrorId, ErrorDeclaration> errorDeclarations;
private final AbstractGeneratorContext<?, ?> generatorContext;
private final boolean publicConstructorEnabled;

public TypesGenerator(AbstractGeneratorContext<?, ?> generatorContext, boolean publicConstructorEnabled) {
public TypesGenerator(AbstractGeneratorContext<?, ?> generatorContext) {
this.errorDeclarations = generatorContext.getIr().getErrors();
this.typeDeclarations = generatorContext.getTypeDeclarations();
this.generatorContext = generatorContext;
this.publicConstructorEnabled = publicConstructorEnabled;
}

public Result generateFiles() {
Expand All @@ -57,8 +55,7 @@ public Result generateFiles() {
typeDeclaration.getName(),
className,
generatedInterfaces,
false,
publicConstructorEnabled));
false));
return maybeGeneratedJavaFile;
})
.filter(Optional::isPresent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public GeneratedJavaFile generateFile() {
.resolvedType(ResolvedTypeReference.primitive(
PrimitiveType.builder().v1(PrimitiveTypeV1.STRING).build()))
.build();
AliasGenerator aliasGenerator = new AliasGenerator(className, generatorContext, aliasTypeDeclaration, false);
AliasGenerator aliasGenerator = new AliasGenerator(className, generatorContext, aliasTypeDeclaration);
return aliasGenerator.generateFile();
}
}
27 changes: 20 additions & 7 deletions generators/java/model/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,26 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.9.3] - 2024-09-04

- Improvement: Public constructors can now be generated for all model types:
```yaml
generators:
- name: fernapi/fern-java-model
config:
enable-public-constructors: true # default false
```
## [0.9.2] - 2024-07-23
* Improvement: Generated builder methods for optional fields can now accept null directly.
- Improvement: Generated builder methods for optional fields can now accept null directly.
## [0.9.1-rc0] - 2024-07-02
* Improvement: The generator now adds a class-level `@JsonInclude(JsonInclude.Include.NON_ABSENT)` annotation to
each generated type in place of the previous `@JsonInclude(JsonInclude.Include.NON_EMPTY)` by default. This is
- Improvement: The generator now adds a class-level `@JsonInclude(JsonInclude.Include.NON_ABSENT)` annotation to
each generated type in place of the previous `@JsonInclude(JsonInclude.Include.NON_EMPTY)` by default. This is
configurable in the `generators.yml` file:
```yaml
```yaml
generators:
- name: fernapi/fern-java-model
config:
Expand All @@ -37,16 +47,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
undiscriminated unions would have failed to compile due to Java's type erasure causing conflicts.

## [0.8.0-rc0] - 2024-05-13

- Chore: Bump intermediate representation to v42

## [0.7.1] - 2024-02-04

- Chore: Bump intermediate representation to v31
- Feature: The generated models now support boolean literals and users
do not have to specify them in the builder.
For example, for the following object
```yaml
Actor:
properties:
Actor:
properties:
name: string
isMale: literal<true>
```
Expand All @@ -56,6 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
var actor = Actor.builder()
.name("Brad Pitt")
.build();
```

## [0.6.1] - 2024-02-03

Expand Down
2 changes: 1 addition & 1 deletion generators/java/model/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.9.2
0.9.3
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private void generateTypes(
this.addGeneratedFile(dateTimeDeserializerGenerator.generateFile());

// types
TypesGenerator typesGenerator = new TypesGenerator(context, false);
TypesGenerator typesGenerator = new TypesGenerator(context);
Result generatedTypes = typesGenerator.generateFiles();
generatedTypes.getTypes().values().forEach(this::addGeneratedFile);
generatedTypes.getInterfaces().values().forEach(this::addGeneratedFile);
Expand Down
51 changes: 32 additions & 19 deletions generators/java/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,39 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.7] - 2024-09-04

- Improvement: Public constructors can now be generated for all model types:
```yaml
generators:
- name: fernapi/fern-java-sdk
config:
enable-public-constructors: true # default false
```
## [1.0.6] - 2024-09-04
- Fix: Fixed a bug where optional collections are not handled properly in paginated responses.
## [1.0.5] - 2024-07-26
* Fix: Fixed a bug where local generation custom config doesn't pick up some values, including exception naming.
- Fix: Fixed a bug where local generation custom config doesn't pick up some values, including exception naming.
## [1.0.4] - 2024-07-24
* Fix: Fixed a bug where OkHttp responses could be closed prematurely.
- Fix: Fixed a bug where OkHttp responses could be closed prematurely.
## [1.0.3] - 2024-07-23
* Improvement: Generated builder methods for optional fields can now accept null directly.
- Improvement: Generated builder methods for optional fields can now accept null directly.
## [1.0.2-rc0] - 2024-07-02
* Improvement: The SDK generator now adds a class-level `@JsonInclude(JsonInclude.Include.NON_ABSENT)` annotation to
each generated type in place of the previous `@JsonInclude(JsonInclude.Include.NON_EMPTY)` by default. This ensures
that required empty collection fields are not removed from request or response json. This is configurable in the
- Improvement: The SDK generator now adds a class-level `@JsonInclude(JsonInclude.Include.NON_ABSENT)` annotation to
each generated type in place of the previous `@JsonInclude(JsonInclude.Include.NON_EMPTY)` by default. This ensures
that required empty collection fields are not removed from request or response json. This is configurable in the
`generators.yml` file:
```yaml
```yaml
generators:
- name: fernapi/fern-java-sdk
config:
Expand All @@ -36,14 +46,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [1.0.1] - 2024-06-26

- Break: The Java SDK is now on major version 1. To take this upgrade without any breaks, please add the below
- Break: The Java SDK is now on major version 1. To take this upgrade without any breaks, please add the below
configuration to your `generators.yml` file:
```yaml
generators:
- name: fernapi/fern-java-sdk
config:
base-api-exception-class-name: ApiError
base-exception-class-name: CompanyException # Optional: This should only be set if default naming is undesirable
- name: fernapi/fern-java-sdk
config:
base-api-exception-class-name: ApiError
base-exception-class-name: CompanyException # Optional: This should only be set if default naming is undesirable
```
- Improvement: We now generate Exception types for all errors that are defined in the IR. Generated clients with an
error discrimination strategy of "status code" will throw one of these typed Exceptions based on the status code of
Expand All @@ -58,7 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.10.1] - 2024-06-13

- Feature: Add support for cursor and offset pagination.
- Feature: Add support for cursor and offset pagination.

For example, consider the following endpoint `/users` endpoint:

Expand Down Expand Up @@ -90,20 +100,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
starting_after: optional<string>
response: ListUsersResponse
```

The generated `SyncPagingIterable<User>` can then be used to traverse through the `User` objects:

```java
for (User user : client.users.list(...)) {
System.out.println(user);
}
```

Or stream them:

```java
client.users.list(...).streamItems().map(user -> ...);
```

Or statically calling `nextPage()` to perform the pagination manually:

```java
SyncPagingIterable<User> pager = client.users.list(...);
// First page
Expand All @@ -120,18 +133,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.9.8] - 2024-06-06

- Fix: `RequestOptions` are now generated with the `timeout` field initialized to `Optional.empty()` instead of `null`
- Fix: `RequestOptions` are now generated with the `timeout` field initialized to `Optional.empty()` instead of `null`
to avoid NPEs if `timeout` is not set in the builder.

## [0.9.7] - 2024-06-06

- Feature: The SDK generator now generates `@java.lang.Override` over `@Override` in all files to avoid clashes with any
`Override.java` class that may have been generated in the same package. The former was used most places, but not all,
`Override.java` class that may have been generated in the same package. The former was used most places, but not all,
until this release.

## [0.9.6] - 2024-06-05

- Feature: The SDK generator now supports returning response properties from client methods rather than just the
- Feature: The SDK generator now supports returning response properties from client methods rather than just the
responses themselves.

## [0.9.5] - 2024-05-30
Expand Down
2 changes: 1 addition & 1 deletion generators/java/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.6
1.0.7
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public GeneratedRootClient generateClient(
this.addGeneratedFile(generatedMediaTypesFile);

// types
TypesGenerator typesGenerator = new TypesGenerator(context, false);
TypesGenerator typesGenerator = new TypesGenerator(context);
Result generatedTypes = typesGenerator.generateFiles();
generatedTypes.getTypes().values().forEach(this::addGeneratedFile);
generatedTypes.getInterfaces().values().forEach(this::addGeneratedFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ public GeneratedWrappedRequest generateFile() {
.collect(Collectors.toList()),
generatorContext,
allGeneratedInterfaces,
className,
false);
className);
GeneratedObject generatedObject = objectGenerator.generateFile();
RequestBodyGetterFactory requestBodyGetterFactory =
new RequestBodyGetterFactory(objectProperties, generatedObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void generateClient(SpringGeneratorContext context, IntermediateRepresent
maybeAuth.ifPresent(this::addGeneratedFile);

// types
TypesGenerator typesGenerator = new TypesGenerator(context, springCustomConfig.enablePublicConstructors());
TypesGenerator typesGenerator = new TypesGenerator(context);
Result generatedTypes = typesGenerator.generateFiles();
generatedTypes.getTypes().values().forEach(this::addGeneratedFile);
generatedTypes.getInterfaces().values().forEach(this::addGeneratedFile);
Expand Down
Loading

0 comments on commit eceb8bd

Please sign in to comment.