Skip to content

Commit

Permalink
read properties from allOf inline schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoMario committed Jan 30, 2024
1 parent 3be9094 commit da321e9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ public abstract class DefaultCodegenConfig implements CodegenConfig {

protected String ignoreFilePathOverride;
protected boolean useOas2 = false;
protected boolean copyFistAllOfProperties = false;
protected boolean ignoreImportMapping;

public List<CliOption> cliOptions() {
Expand Down Expand Up @@ -1406,29 +1405,24 @@ else if (schema instanceof ComposedSchema) {
final String parentName = getParentName(composed);
final Schema parent = StringUtils.isBlank(parentName) ? null : allDefinitions.get(parentName);
final List<Schema> allOf = composed.getAllOf();
// interfaces (intermediate models)
if (allOf != null && !allOf.isEmpty()) {
for (int i = 0; i < allOf.size(); i++) {
if (i == 0 && !copyFistAllOfProperties) {
continue;
}
Schema interfaceSchema = allOf.get(i);
if (StringUtils.isBlank(interfaceSchema.get$ref())) {
continue;
}
Schema refSchema = null;
String ref = OpenAPIUtil.getSimpleRef(interfaceSchema.get$ref());
if (allDefinitions != null) {
refSchema = allDefinitions.get(ref);
final int index = copyFirstAllOfProperties(allOf.get(0)) ? 0 : 1;
for (int i = index; i < allOf.size(); i++) {
Schema allOfSchema = allOf.get(i);
if (StringUtils.isNotBlank(allOfSchema.get$ref())) {
String ref = OpenAPIUtil.getSimpleRef(allOfSchema.get$ref());
if (allDefinitions != null) {
allOfSchema = allDefinitions.get(ref);
}
final String modelName = toModelName(ref);
addImport(codegenModel, modelName);
}
final String modelName = toModelName(ref);
addImport(codegenModel, modelName);
if (allDefinitions != null && refSchema != null) {
if (allDefinitions != null && allOfSchema != null) {
if (!supportsMixins) {
addProperties(properties, required, refSchema, allDefinitions);
addProperties(properties, required, allOfSchema, allDefinitions);
}
if (supportsInheritance) {
addProperties(allProperties, allRequired, refSchema, allDefinitions);
addProperties(allProperties, allRequired, allOfSchema, allDefinitions);
}
}
}
Expand Down Expand Up @@ -1477,6 +1471,11 @@ else if (schema instanceof ComposedSchema) {
return codegenModel;
}

protected boolean copyFirstAllOfProperties(Schema allOfSchema) {
return StringUtils.isBlank(allOfSchema.get$ref())
&& allOfSchema.getProperties() != null && !allOfSchema.getProperties().isEmpty();
}

protected void processMapSchema(CodegenModel codegenModel, String name, Schema schema) {
codegenModel.getVendorExtensions().put(CodegenConstants.IS_MAP_CONTAINER_EXT_NAME, Boolean.TRUE);
codegenModel.getVendorExtensions().put(IS_CONTAINER_EXT_NAME, Boolean.TRUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public void processOpts() {
if (StringUtils.isBlank(templateDir)) {
embeddedTemplateDir = templateDir = getTemplateDir();
}

// Setup project name
if (additionalProperties.containsKey(PROJECT_NAME)) {
setProjectName((String) additionalProperties.get(PROJECT_NAME));
Expand Down Expand Up @@ -348,9 +348,6 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("gitignore.mustache",
"",
".gitignore"));

copyFistAllOfProperties = true;

}

@Override
Expand Down Expand Up @@ -852,5 +849,10 @@ private static void reconcileProperties(CodegenModel codegenModel,
codegenModel.vars = codegenProperties;
}
}

@Override
protected boolean copyFirstAllOfProperties(Schema allOfSchema) {
return false;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {{invokerPackage}}.Configuration;
import {{invokerPackage}}.Pair;

{{#jakarta}}
import jakarta.ws.rs.core.GenericType;
import jakarta.ws.rs.core.GenericType;
{{/jakarta}}
{{^jakarta}}
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.GenericType;
{{/jakarta}}

{{#imports}}import {{import}};
Expand Down
24 changes: 12 additions & 12 deletions src/main/resources/handlebars/Java/typeInfoAnnotation.mustache
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{{#jackson}}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "{{discriminator.propertyName}}", visible = true )
@JsonSubTypes({
{{#if discriminator.mapping}}
{{#each discriminator.mapping}}
@JsonSubTypes.Type(value = {{this}}.class, name = "{{@key}}"),
{{/each}}
{{else}}
{{#children}}
@JsonSubTypes.Type(value = {{classname}}.class, name = "{{name}}"),
{{/children}}
{{/if}}
})
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "{{discriminator.propertyName}}", visible = true )
@JsonSubTypes({
{{#if discriminator.mapping}}
{{#each discriminator.mapping}}
@JsonSubTypes.Type(value = {{this}}.class, name = "{{@key}}"),
{{/each}}
{{else}}
{{#children}}
@JsonSubTypes.Type(value = {{classname}}.class, name = "{{name}}"),
{{/children}}
{{/if}}
})
{{/jackson}}

0 comments on commit da321e9

Please sign in to comment.