Skip to content

Commit

Permalink
fix(core): single value enums (const) are not generated correctly i…
Browse files Browse the repository at this point in the history
…n `3.1` specs (#19696)

* fix(core): single value enums (`const`) are not generated correctly in `3.1` specs

* fix: const enums

* chore: update samples

* chore: update samples
  • Loading branch information
joscha authored Oct 6, 2024
1 parent 899ddec commit 2f73582
Show file tree
Hide file tree
Showing 31 changed files with 1,396 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bin/configs/typescript-consolidated-single-value-enum.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
generatorName: typescript
outputDir: samples/client/others/typescript/builds/enum-single-value
inputSpec: modules/openapi-generator/src/test/resources/3_1/enum-single-value.yaml
templateDir: modules/openapi-generator/src/main/resources/typescript
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,12 @@ private Schema processNormalize31Spec(Schema schema, Set<Schema> visitedSchemas)
schema.getTypes().remove("null");
}

// process const
if (schema.getConst() != null) {
schema.setEnum(Arrays.asList(schema.getConst()));
schema.setConst(null);
}

// only one item (type) left
if (schema.getTypes().size() == 1) {
String type = String.valueOf(schema.getTypes().iterator().next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -758,4 +758,29 @@ public void testOpenAPINormalizerSimplifyOneOfAnyOf31Spec() {
assertEquals(schema18.getOneOf(), null);
assertEquals(schema18.get$ref(), "#/components/schemas/Parent");
}

@Test
public void testOpenAPINormalizerSingleConstEnum31Spec() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_1/enum-single-value.yaml");

Schema reference_3_0 = openAPI.getComponents().getSchemas().get("SingleValueEnum_3_0");
assertEquals(((Schema) reference_3_0.getProperties().get("type")).getEnum().size(), 1);

Schema schema = openAPI.getComponents().getSchemas().get("SingleValueEnum_3_1");
Schema originalTypeSchema = (Schema) schema.getProperties().get("type");
assertFalse(ModelUtils.isEnumSchema(originalTypeSchema));
var originalConst = originalTypeSchema.getConst();
assertNotNull(originalConst);

Map<String, String> inputRules = Map.of("NORMALIZE_31SPEC", "true");
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, inputRules);
openAPINormalizer.normalize();

Schema schema2 = openAPI.getComponents().getSchemas().get("SingleValueEnum_3_1");
Schema normalizedTypeSchema = (Schema) schema2.getProperties().get("type");
assertTrue(ModelUtils.isEnumSchema(normalizedTypeSchema));
assertNull(normalizedTypeSchema.getConst());
assertEquals(normalizedTypeSchema.getEnum().size(), 1);
assertEquals(Arrays.asList(originalConst), normalizedTypeSchema.getEnum());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
openapi: 3.1.0
info:
title: ""
version: 1.0.0
components:
schemas:
SingleValueEnum_3_1:
required:
- type
properties:
type:
const: this-is-my-only-value
type: string
SingleValueEnum_3_0:
required:
- type
properties:
type:
enum:
- this-is-my-only-value

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2f73582

Please sign in to comment.