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
Original file line number Diff line number Diff line change
Expand Up @@ -4107,7 +4107,9 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
if (p.getWriteOnly() != null) {
property.isWriteOnly = p.getWriteOnly();
}
if (p.getNullable() != null) {
if (ModelUtils.isNullable(p)) {
property.isNullable = true;
} else if (p.getNullable() != null) {
property.isNullable = p.getNullable();
}

Expand Down Expand Up @@ -4159,7 +4161,9 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
}

// set isNullable using nullable or x-nullable in the schema
if (referencedSchema.getNullable() != null) {
if (ModelUtils.isNullable(referencedSchema)) {
property.isNullable = true;
} else if (referencedSchema.getNullable() != null) {
property.isNullable = referencedSchema.getNullable();
} else if (referencedSchema.getExtensions() != null &&
referencedSchema.getExtensions().containsKey(X_NULLABLE)) {
Expand Down Expand Up @@ -4262,7 +4266,9 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo
if (original != null) {
p = original;
// evaluate common attributes if defined in the top level
if (p.getNullable() != null) {
if (ModelUtils.isNullable(p)) {
property.isNullable = true;
} else if (p.getNullable() != null) {
property.isNullable = p.getNullable();
} else if (p.getExtensions() != null && p.getExtensions().containsKey(X_NULLABLE)) {
property.isNullable = (Boolean) p.getExtensions().get(X_NULLABLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,9 @@ public static boolean isNullable(Schema schema) {
if (schema.getExtensions() != null && schema.getExtensions().get(X_NULLABLE) != null) {
return Boolean.parseBoolean(schema.getExtensions().get(X_NULLABLE).toString());
}
if (schema.getTypes() != null && schema.getTypes().contains("null")) {
return true;
}
// In OAS 3.1, the recommended way to define a nullable property or object is to use oneOf.
if (isComposedSchema(schema)) {
return isNullableComposedSchema(schema);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,28 @@ public void testDeprecatedArrayAttribute() throws Exception {
TestUtils.assertFileContains(file, "'nicknames'?: Array<string>");
}

@Test
public void generatesNullUnionsForOpenApi31NullableContainers() throws Exception {
final File output = Files.createTempDirectory("typescript_axios_nullable_container_types_").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("typescript-axios")
.setInputSpec("src/test/resources/3_1/typescript-axios/nullable-container-types.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

final ClientOptInput clientOptInput = configurator.toClientOptInput();
final DefaultGenerator generator = new DefaultGenerator();
final List<File> files = generator.opts(clientOptInput).generate();
files.forEach(File::deleteOnExit);

Path file = Paths.get(output + "/api.ts");

TestUtils.assertFileContains(file, "'validation'?: { [key: string]: string; } | null;");
TestUtils.assertFileContains(file, "'requirements'?: Array<string> | null;");
TestUtils.assertFileContains(file, "'settings': MappingItemResourceSettings | null;");
}

@Test
public void generatesTrailingCommasInAsConstEnumObjects() throws Exception {
final File output = Files.createTempDirectory("typescript_axios_trailing_commas_").toFile();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
openapi: 3.1.0
info:
title: Nullable container types
version: 1.0.0
paths:
/mappings:
get:
operationId: listMappings
responses:
'200':
description: successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/MappingItemResource'
components:
schemas:
MappingItemResource:
type: object
properties:
settings:
type:
- object
- 'null'
properties:
validation:
type:
- object
- 'null'
additionalProperties:
type: string
requirements:
type:
- array
- 'null'
items:
type: string
required:
- settings
Loading