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 @@ -787,13 +787,9 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}

List<CodegenProperty> codegenProperties = new ArrayList<>();
if (model.getComposedSchemas() == null || (model.getComposedSchemas() != null && model.getComposedSchemas().getAllOf() != null)) {
// If the model is an allOf or does not have any composed schemas, then we can use the model's properties.
if (model.vars != null && !model.vars.isEmpty()) {
codegenProperties.addAll(model.vars);
} else {
// If the model is no model, but is a
// anyOf or oneOf, add all first level options
// from anyOf or oneOf.
codegenProperties.addAll(inheritedProperties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,4 +562,34 @@ public void testOneOfUnmarshalJSONGeneratedByDefault() throws IOException {
"validator.Validate",
"gopkg.in/validator.v2");
}

@Test(description = "schema with both properties and oneOf must emit json tags on properties (#24916)")
public void testOneOfWithPropertiesEmitsJsonTags() throws IOException {
File output = Files.createTempDirectory("test").toFile();
output.deleteOnExit();

final CodegenConfigurator configurator = new CodegenConfigurator()
.setGeneratorName("go")
.setInputSpec("src/test/resources/3_0/go/oneof-with-properties.yaml")
.setOutputDir(output.getAbsolutePath().replace("\\", "/"));

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

Path modelFile = Paths.get(output + "/model_thing.go");
TestUtils.assertFileExists(modelFile);

// Properties must have json tags even though oneOf is present
TestUtils.assertFileContains(modelFile,
"Kind string `json:\"kind\"`");
TestUtils.assertFileContains(modelFile,
"FirstValue []float32 `json:\"first_value,omitempty\"`");
TestUtils.assertFileContains(modelFile,
"SecondValue []float32 `json:\"second_value,omitempty\"`");

// Must not be rendered as a oneOf union struct
TestUtils.assertFileNotContains(modelFile,
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
"GetActualInstance");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
openapi: 3.0.3
info:
title: oneOf with properties regression test
version: 1.0.0
paths:
/thing:
get:
operationId: getThing
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Thing'
components:
schemas:
Thing:
type: object
required:
- kind
properties:
kind:
type: string
first_value:
type: array
items:
type: number
second_value:
type: array
items:
type: number
oneOf:
- required:
- first_value
not:
required:
- second_value
- required:
- second_value
not:
required:
- first_value
Loading