Skip to content

Commit

Permalink
chore(gradle/openapi-sdk): update to OpenAPI 6.0.1 and update Microna…
Browse files Browse the repository at this point in the history
…ut codegen
  • Loading branch information
meriouma committed Aug 30, 2022
1 parent f85f68a commit 7fe6d96
Show file tree
Hide file tree
Showing 10 changed files with 562 additions and 1,511 deletions.
6 changes: 3 additions & 3 deletions gradle/openapi-sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ val openApiGenerator by configurations.creating {
}

dependencies {
val openApiVersion = "6.0.0"
val openApiVersion = "6.0.1"

compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21")
implementation("org.openapitools:openapi-generator-gradle-plugin:$openApiVersion")
implementation("com.equisoft.openapi.generator.micronaut:micronaut-project-openapi-generator:0.4.0")
implementation("com.equisoft.openapi.generator.micronaut:micronaut-project-openapi-generator:0.5.0-SNAPSHOT")

openApiGenerator("org.openapitools:openapi-generator:$openApiVersion")
}

java {
sourceCompatibility = JavaVersion.VERSION_13
sourceCompatibility = JavaVersion.VERSION_17
}

gradlePlugin {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class InlineModelResolver {
private Map<String, Schema> addedModels = new HashMap<>();
private Map<String, String> generatedSignature = new HashMap<>();
private Map<String, String> inlineSchemaNameMapping = new HashMap<>();
private Map<String, String> inlineSchemaNameDefaults = new HashMap<>();
private Set<String> inlineSchemaNameMappingValues = new HashSet<>();
public boolean resolveInlineEnums = false;

Expand All @@ -60,11 +61,20 @@ public class InlineModelResolver {

final Logger LOGGER = LoggerFactory.getLogger(InlineModelResolver.class);

public InlineModelResolver() {
this.inlineSchemaNameDefaults.put("arrayItemSuffix", "_inner");
this.inlineSchemaNameDefaults.put("mapItemSuffix", "_value");
}

public void setInlineSchemaNameMapping(Map inlineSchemaNameMapping) {
this.inlineSchemaNameMapping = inlineSchemaNameMapping;
this.inlineSchemaNameMappingValues = new HashSet<>(inlineSchemaNameMapping.values());
}

public void setInlineSchemaNameDefaults(Map inlineSchemaNameDefaults) {
this.inlineSchemaNameDefaults.putAll(inlineSchemaNameDefaults);
}

void flatten(OpenAPI openAPI) {
this.openAPI = openAPI;

Expand Down Expand Up @@ -123,9 +133,9 @@ private void flattenPaths() {
Map<String, Callback> callbacks = operation.getCallbacks();
if (callbacks != null) {
operations.addAll(callbacks.values().stream()
.flatMap(callback -> callback.values().stream())
.flatMap(pathItem -> pathItem.readOperations().stream())
.collect(Collectors.toList()));
.flatMap(callback -> callback.values().stream())
.flatMap(pathItem -> pathItem.readOperations().stream())
.collect(Collectors.toList()));
}
}

Expand Down Expand Up @@ -172,7 +182,7 @@ private boolean isModelNeeded(Schema schema) {
if (m.getAnyOf() != null && !m.getAnyOf().isEmpty()) {
return true;
}
if (m.getOneOf() != null && !m.getOneOf().isEmpty() && !isNullableOneOfComposedSchema(m)) {
if (m.getOneOf() != null && !m.getOneOf().isEmpty() && !isNullableOneOfComposedSchema(m)) { // Equisoft patch
return true;
}
}
Expand All @@ -192,10 +202,10 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
// if ref already, no inline schemas should be present but check for
// any to catch OpenAPI violations
if (isModelNeeded(schema) || "object".equals(schema.getType()) ||
schema.getProperties() != null || schema.getAdditionalProperties() != null ||
schema instanceof ComposedSchema) {
schema.getProperties() != null || schema.getAdditionalProperties() != null ||
schema instanceof ComposedSchema) {
LOGGER.error("Illegal schema found with $ref combined with other properties," +
" no properties should be defined alongside a $ref:\n " + schema.toString());
" no properties should be defined alongside a $ref:\n " + schema.toString());
}
return;
}
Expand All @@ -218,7 +228,7 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
} else if (prop instanceof ComposedSchema) {
ComposedSchema m = (ComposedSchema) prop;
if (m.getAllOf() != null && m.getAllOf().size() == 1 &&
!(m.getAllOf().get(0).getType() == null || "object".equals(m.getAllOf().get(0).getType()))) {
!(m.getAllOf().get(0).getType() == null || "object".equals(m.getAllOf().get(0).getType()))) {
// allOf with only 1 type (non-model)
LOGGER.info("allOf schema used by the property `{}` replaced by its only item (a type)", propName);
props.put(propName, m.getAllOf().get(0));
Expand All @@ -230,7 +240,7 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
if (schema.getAdditionalProperties() != null) {
if (schema.getAdditionalProperties() instanceof Schema) {
Schema inner = (Schema) schema.getAdditionalProperties();
String schemaName = resolveModelName(schema.getTitle(), modelPrefix + "_value");
String schemaName = resolveModelName(schema.getTitle(), modelPrefix + this.inlineSchemaNameDefaults.get("mapItemSuffix"));
// Recurse to create $refs for inner models
gatherInlineModels(inner, schemaName);
if (isModelNeeded(inner)) {
Expand All @@ -243,13 +253,13 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
} else if (schema.getProperties() != null) {
// If non-object type is specified but also properties
LOGGER.error("Illegal schema found with non-object type combined with properties," +
" no properties should be defined:\n " + schema.toString());
" no properties should be defined:\n " + schema.toString());
return;
} else if (schema.getAdditionalProperties() != null) {
// If non-object type is specified but also additionalProperties
LOGGER.error("Illegal schema found with non-object type combined with" +
" additionalProperties, no additionalProperties should be defined:\n " +
schema.toString());
" additionalProperties, no additionalProperties should be defined:\n " +
schema.toString());
return;
}
// Check array items
Expand All @@ -262,10 +272,10 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
}*/
if (items == null) {
LOGGER.error("Illegal schema found with array type but no items," +
" items must be defined for array schemas:\n " + schema.toString());
" items must be defined for array schemas:\n " + schema.toString());
return;
}
String schemaName = resolveModelName(items.getTitle(), modelPrefix + "_inner");
String schemaName = resolveModelName(items.getTitle(), modelPrefix + this.inlineSchemaNameDefaults.get("arrayItemSuffix"));

// Recurse to create $refs for inner models
gatherInlineModels(items, schemaName);
Expand Down Expand Up @@ -322,7 +332,7 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
}
m.setAnyOf(newAnyOf);
}
if (m.getOneOf() != null && !isNullableOneOfComposedSchema(m)) {
if (m.getOneOf() != null && !isNullableOneOfComposedSchema(m)) { // Equisoft patch
List<Schema> newOneOf = new ArrayList<Schema>();
for (Schema inner : m.getOneOf()) {
String schemaName = resolveModelName(inner.getTitle(), modelPrefix + "_oneOf");
Expand Down Expand Up @@ -402,7 +412,7 @@ private void flattenRequestBody(String modelName, Operation operation) {
}

flattenContent(requestBody.getContent(),
(operation.getOperationId() == null ? modelName : operation.getOperationId()) + "_request");
(operation.getOperationId() == null ? modelName : operation.getOperationId()) + "_request");
}

/**
Expand All @@ -428,7 +438,7 @@ private void flattenParameters(String modelName, Operation operation) {
continue;
}
String schemaName = resolveModelName(parameterSchema.getTitle(),
(operation.getOperationId() == null ? modelName : operation.getOperationId()) + "_" + parameter.getName() + "_parameter");
(operation.getOperationId() == null ? modelName : operation.getOperationId()) + "_" + parameter.getName() + "_parameter");
// Recursively gather/make inline models within this schema if any
gatherInlineModels(parameterSchema, schemaName);
if (isModelNeeded(parameterSchema)) {
Expand Down Expand Up @@ -456,7 +466,7 @@ private void flattenResponses(String modelName, Operation operation) {
ApiResponse response = responsesEntry.getValue();

flattenContent(response.getContent(),
(operation.getOperationId() == null ? modelName : operation.getOperationId()) + "_" + key + "_response");
(operation.getOperationId() == null ? modelName : operation.getOperationId()) + "_" + key + "_response");
}
}

Expand Down Expand Up @@ -494,9 +504,9 @@ private void flattenComposedChildren(String key, List<Schema> children) {
while (listIterator.hasNext()) {
Schema component = listIterator.next();
if ((component != null) &&
(component.get$ref() == null) &&
((component.getProperties() != null && !component.getProperties().isEmpty()) ||
(component.getEnum() != null && !component.getEnum().isEmpty()))) {
(component.get$ref() == null) &&
((component.getProperties() != null && !component.getProperties().isEmpty()) ||
(component.getEnum() != null && !component.getEnum().isEmpty()))) {
// If a `title` attribute is defined in the inline schema, codegen uses it to name the
// inline schema. Otherwise, we'll use the default naming such as InlineObject1, etc.
// We know that this is not the best way to name the model.
Expand Down Expand Up @@ -623,8 +633,8 @@ private void addGenerated(String name, Schema model) {
*/
private String sanitizeName(final String name) {
return name
.replaceAll("^[0-9]", "_$0") // e.g. 12object => _12object
.replaceAll("[^A-Za-z0-9]", "_"); // e.g. io.schema.User name => io_schema_User_name
.replaceAll("^[0-9]", "_$0") // e.g. 12object => _12object
.replaceAll("[^A-Za-z0-9]", "_"); // e.g. io.schema.User name => io_schema_User_name
}

/**
Expand Down Expand Up @@ -657,7 +667,7 @@ private void flattenProperties(OpenAPI openAPI, Map<String, Schema> properties,
String key = propertiesEntry.getKey();
Schema property = propertiesEntry.getValue();
if (property instanceof ObjectSchema && ((ObjectSchema) property).getProperties() != null
&& ((ObjectSchema) property).getProperties().size() > 0) {
&& ((ObjectSchema) property).getProperties().size() > 0) {
ObjectSchema op = (ObjectSchema) property;
String modelName = resolveModelName(op.getTitle(), path + "_" + key);
Schema model = modelFromProperty(openAPI, op, modelName);
Expand Down
Loading

0 comments on commit 7fe6d96

Please sign in to comment.