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
Original file line number Diff line number Diff line change
Expand Up @@ -8086,7 +8086,8 @@ protected void addBodyModelSchema(CodegenParameter codegenParameter, String name
} else {
CodegenProperty codegenProperty = fromProperty("property", schema, false);

if (codegenProperty != null && codegenProperty.getComplexType() != null && codegenProperty.getComplexType().contains(" | ")) {
// inline unions only: a named model (e.g. a $ref oneOf of primitives) keeps its name below
if (codegenModel == null && codegenProperty != null && codegenProperty.getComplexType() != null && codegenProperty.getComplexType().contains(" | ")) {
// TODO move this splitting logic to the generator that needs it only
List<String> parts = Arrays.asList(codegenProperty.getComplexType().split(" \\| "));
imports.addAll(parts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "{{description}}",
"author": "{{author}}",
"license": "{{licenseName}}",
"peeerDependencies": {
"peerDependencies": {
"@nestjs/common": "^{{nestVersion}}",
"@nestjs/core": "^{{nestVersion}}",
"@nestjs/platform-express": "^{{nestVersion}}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.openapitools.codegen.config.CodegenConfigurator;
import org.openapitools.codegen.config.GlobalSettings;
import org.openapitools.codegen.languages.SpringCodegen;
import org.openapitools.codegen.languages.TypeScriptClientCodegen;
import org.openapitools.codegen.model.EnumVarMap;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
Expand Down Expand Up @@ -2927,6 +2928,26 @@ public void arrayInnerReferencedSchemaMarkedAsModel_30() {
assertFalse(codegenParameter.items.isContainer);
}

// TypeScript names unions "number | string", which must not replace the model name of a $ref body
@Test
public void referencedPrimitiveUnionBodyKeepsModelName() {
final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/composed-primitive-request-body.yaml");
final DefaultCodegen codegen = new TypeScriptClientCodegen();
codegen.setOpenAPI(openAPI);

Map.of("/one-of", "NodeId", "/any-of", "NodeKey").forEach((path, modelName) -> {
Set<String> imports = new HashSet<>();
RequestBody body = openAPI.getPaths().get(path).getPost().getRequestBody();

CodegenParameter codegenParameter = codegen.fromRequestBody(body, imports, "");

assertEquals(modelName, codegenParameter.dataType);
assertEquals(modelName, codegenParameter.baseType);
assertEquals(codegen.toParamName(modelName), codegenParameter.paramName);
assertEquals(Set.of(modelName), imports);
});
}

@Test
@SuppressWarnings("unchecked")
public void commonLambdasRegistrationTest() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
openapi: 3.0.3
info:
title: Request bodies that are unions of primitives
version: 1.0.0
paths:
/one-of:
post:
operationId: lookupByOneOf
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/NodeId'
responses:
'200':
description: ok
/any-of:
post:
operationId: lookupByAnyOf
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/NodeKey'
responses:
'200':
description: ok
components:
schemas:
NodeId:
oneOf:
- type: integer
format: int32
- type: string
NodeKey:
anyOf:
- type: integer
format: int32
- type: string
Loading