-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
[protobuf-schema] Add flag to handle complex type #20915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9dbf165
Wrap Complex Type
lucy66hw a2abdc1
update
lucy66hw 9b7bcf0
Add java doc and update test
lucy66hw fc8fe61
change default wrapComplexType to true
lucy66hw e957b2a
add protobuf-schema-config-complex to CI
lucy66hw fd6badf
add service proto to address CI failure
lucy66hw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,10 @@ | ||
| generatorName: protobuf-schema | ||
| outputDir: samples/config/petstore/protobuf-schema-config | ||
| inputSpec: modules/openapi-generator/src/test/resources/3_0/protobuf/petstore.yaml | ||
| inputSpec: modules/openapi-generator/src/test/resources/3_0/protobuf/petstore-complex.yaml | ||
| templateDir: modules/openapi-generator/src/main/resources/protobuf-schema | ||
| additionalProperties: | ||
| packageName: petstore | ||
| addJsonNameAnnotation: true | ||
| numberedFieldNumberList: true | ||
| startEnumsWithUnspecified: true | ||
| wrapComplexType: true | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,10 @@ | |
|
|
||
| package org.openapitools.codegen.languages; | ||
|
|
||
| import io.swagger.v3.oas.models.OpenAPI; | ||
| import io.swagger.v3.oas.models.media.ArraySchema; | ||
| import io.swagger.v3.oas.models.media.MapSchema; | ||
| import io.swagger.v3.oas.models.media.ObjectSchema; | ||
| import io.swagger.v3.oas.models.media.Schema; | ||
| import lombok.Setter; | ||
| import org.apache.commons.lang3.StringUtils; | ||
|
|
@@ -58,6 +62,8 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf | |
|
|
||
| public static final String ADD_JSON_NAME_ANNOTATION = "addJsonNameAnnotation"; | ||
|
|
||
| public static final String WRAP_COMPLEX_TYPE = "wrapComplexType"; | ||
|
|
||
| private final Logger LOGGER = LoggerFactory.getLogger(ProtobufSchemaCodegen.class); | ||
|
|
||
| @Setter protected String packageName = "openapitools"; | ||
|
|
@@ -68,6 +74,8 @@ public class ProtobufSchemaCodegen extends DefaultCodegen implements CodegenConf | |
|
|
||
| private boolean addJsonNameAnnotation = false; | ||
|
|
||
| private boolean wrapComplexType = false; | ||
|
|
||
| @Override | ||
| public CodegenType getTag() { | ||
| return CodegenType.SCHEMA; | ||
|
|
@@ -177,6 +185,7 @@ public ProtobufSchemaCodegen() { | |
| addSwitch(NUMBERED_FIELD_NUMBER_LIST, "Field numbers in order.", numberedFieldNumberList); | ||
| addSwitch(START_ENUMS_WITH_UNSPECIFIED, "Introduces \"UNSPECIFIED\" as the first element of enumerations.", startEnumsWithUnspecified); | ||
| addSwitch(ADD_JSON_NAME_ANNOTATION, "Append \"json_name\" annotation to message field when the specification name differs from the protobuf field name", addJsonNameAnnotation); | ||
| addSwitch(WRAP_COMPLEX_TYPE, "Generate Additional message for complex type", wrapComplexType); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -215,6 +224,10 @@ public void processOpts() { | |
| this.addJsonNameAnnotation = convertPropertyToBooleanAndWriteBack(ADD_JSON_NAME_ANNOTATION); | ||
| } | ||
|
|
||
| if (additionalProperties.containsKey(this.WRAP_COMPLEX_TYPE)) { | ||
| this.wrapComplexType = convertPropertyToBooleanAndWriteBack(WRAP_COMPLEX_TYPE); | ||
| } | ||
|
|
||
| supportingFiles.add(new SupportingFile("README.mustache", "", "README.md")); | ||
| } | ||
|
|
||
|
|
@@ -234,6 +247,195 @@ public String toOperationId(String operationId) { | |
| return camelize(sanitizeName(operationId)); | ||
| } | ||
|
|
||
| /** | ||
| * Creates an array schema from the provided object schema. | ||
| */ | ||
| public Schema createArraySchema(Schema objectSchema) { | ||
| ArraySchema arraySchema = new ArraySchema(); | ||
| arraySchema.items(objectSchema); | ||
| return arraySchema; | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Creates a map schema from the provided object schema. | ||
| */ | ||
| public Schema createMapSchema(Schema objectSchema) { | ||
| MapSchema mapSchema = new MapSchema(); | ||
| mapSchema.additionalProperties(objectSchema); | ||
| return mapSchema; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new model schema for a property. | ||
| */ | ||
| public Schema addSchemas(Schema schema, String schemaName, Set<Schema> visitedSchema) { | ||
| LOGGER.info("Generating new model: {}", schemaName); | ||
|
|
||
| ObjectSchema model = new ObjectSchema(); | ||
| model.setName(schemaName); | ||
|
|
||
| Map<String, Schema> properties = new HashMap<>(); | ||
| properties.put(toVarName(schemaName), schema); | ||
| model.setProperties(properties); | ||
|
|
||
| Schema refSchema = new Schema(); | ||
| refSchema.set$ref("#/components/schemas/" + schemaName); | ||
| refSchema.setName(schemaName); | ||
|
|
||
| visitedSchema.add(refSchema); | ||
|
|
||
| openAPI.getComponents().addSchemas(schemaName, model); | ||
|
|
||
| return refSchema; | ||
| } | ||
|
|
||
| public String getType(Schema schema) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor suggestion: add a doc string explaining what this function does |
||
| if (!ModelUtils.isPrimitiveType(schema)) return ""; | ||
| if(ModelUtils.isNumberSchema(schema)) { | ||
| if(schema.getFormat() != null) { | ||
| return schema.getFormat(); | ||
| } else if (typeMapping.get(schema.getType()) != null) { | ||
| return typeMapping.get(schema.getType()); | ||
| } | ||
| } | ||
| return ModelUtils.getType(schema); | ||
| } | ||
|
|
||
| /** | ||
| * Recursively generates schemas for nested maps and arrays | ||
| */ | ||
| public Schema generateNestedSchema(Schema schema, Set<Schema> visitedSchemas) { | ||
| if (visitedSchemas.contains(schema)) { | ||
| LOGGER.warn("Skipping recursive schema"); | ||
| return schema; | ||
| } | ||
|
|
||
| if(ModelUtils.isArraySchema(schema)) { | ||
| Schema itemsSchema = ModelUtils.getSchemaItems(schema); | ||
| itemsSchema = ModelUtils.getReferencedSchema(openAPI, itemsSchema); | ||
| if(ModelUtils.isModel(itemsSchema)) { | ||
| String newSchemaName = ModelUtils.getSimpleRef(ModelUtils.getSchemaItems(schema).get$ref()) + ARRAY_SUFFIX; | ||
| return addSchemas(schema, newSchemaName, visitedSchemas); | ||
| }else if (ModelUtils.isPrimitiveType(itemsSchema)){ | ||
| String newSchemaName = getType(itemsSchema) + ARRAY_SUFFIX; | ||
| return addSchemas(schema, newSchemaName, visitedSchemas); | ||
| } else { | ||
| Schema childSchema = generateNestedSchema(itemsSchema, visitedSchemas); | ||
| String newSchemaName = childSchema.getName() + ARRAY_SUFFIX; | ||
| Schema arrayModel = createArraySchema(childSchema); | ||
| return addSchemas(arrayModel, newSchemaName, visitedSchemas); | ||
| } | ||
| } else if(ModelUtils.isMapSchema(schema)) { | ||
| Schema mapValueSchema = ModelUtils.getAdditionalProperties(schema); | ||
| mapValueSchema = ModelUtils.getReferencedSchema(openAPI, mapValueSchema); | ||
| if(ModelUtils.isModel(mapValueSchema) ) { | ||
| String newSchemaName = ModelUtils.getSimpleRef(ModelUtils.getAdditionalProperties(schema).get$ref()) + MAP_SUFFIX; | ||
| return addSchemas(schema, newSchemaName, visitedSchemas); | ||
| }else if (ModelUtils.isPrimitiveType(mapValueSchema)){ | ||
| String newSchemaName = getType(mapValueSchema) + MAP_SUFFIX; | ||
| return addSchemas(schema, newSchemaName, visitedSchemas); | ||
| } else { | ||
| Schema innerSchema = generateNestedSchema(mapValueSchema, visitedSchemas); | ||
| String newSchemaName = innerSchema.getName() + MAP_SUFFIX; | ||
| Schema mapModel = createMapSchema(innerSchema); | ||
| return addSchemas(mapModel, newSchemaName, visitedSchemas); | ||
| } | ||
| } | ||
| return schema; | ||
| } | ||
|
|
||
| /** | ||
| * Processes nested schemas for maps and arrays. | ||
| */ | ||
| public void processNestedSchemas(Schema schema, Set<Schema> visitedSchemas) { | ||
| if (ModelUtils.isMapSchema(schema) && ModelUtils.getAdditionalProperties(schema) != null) { | ||
| Schema mapValueSchema = ModelUtils.getAdditionalProperties(schema); | ||
| mapValueSchema = ModelUtils.getReferencedSchema(openAPI, mapValueSchema); | ||
| if (ModelUtils.isArraySchema(mapValueSchema) || ModelUtils.isMapSchema(mapValueSchema)) { | ||
| Schema innerSchema = generateNestedSchema(mapValueSchema, visitedSchemas); | ||
| schema.setAdditionalProperties(innerSchema); | ||
|
|
||
| } | ||
| } else if (ModelUtils.isArraySchema(schema) && ModelUtils.getSchemaItems(schema) != null) { | ||
| Schema arrayItemSchema = ModelUtils.getSchemaItems(schema); | ||
| arrayItemSchema = ModelUtils.getReferencedSchema(openAPI, arrayItemSchema); | ||
| if (ModelUtils.isMapSchema(arrayItemSchema) || ModelUtils.isArraySchema(arrayItemSchema)) { | ||
| Schema innerSchema = generateNestedSchema(arrayItemSchema, visitedSchemas); | ||
| schema.setItems(innerSchema); | ||
| } | ||
| } else if (ModelUtils.isOneOf(schema) && schema.getOneOf() != null) { | ||
| List<Schema> oneOfs = schema.getOneOf(); | ||
| List<Schema> newOneOfs = new ArrayList<>(); | ||
| for (Schema oneOf : oneOfs) { | ||
| Schema oneOfSchema = ModelUtils.getReferencedSchema(openAPI, oneOf); | ||
| if (ModelUtils.isArraySchema(oneOfSchema)) { | ||
| Schema innerSchema = generateNestedSchema(oneOfSchema, visitedSchemas); | ||
| innerSchema.setTitle(oneOf.getTitle()); | ||
| newOneOfs.add(innerSchema); | ||
| } else if (ModelUtils.isMapSchema(oneOfSchema)) { | ||
| Schema innerSchema = generateNestedSchema(oneOfSchema, visitedSchemas); | ||
| innerSchema.setTitle(oneOf.getTitle()); | ||
| newOneOfs.add(innerSchema); | ||
| } else { | ||
| newOneOfs.add(oneOf); | ||
| } | ||
| } | ||
| schema.setOneOf(newOneOfs); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Wraps models to handle nested schemas for maps and arrays. | ||
| */ | ||
| private void wrapModels() { | ||
| Map<String, Schema> models = openAPI.getComponents().getSchemas(); | ||
| Set<Schema> visitedSchema = new HashSet<>(); | ||
| List<String> modelNames = new ArrayList<String>(models.keySet()); | ||
| for (String modelName: modelNames) { | ||
| Schema schema = models.get(modelName); | ||
| processNestedSchemas(schema, visitedSchema); | ||
| if (ModelUtils.isModel(schema) && schema.getProperties() != null) { | ||
| Map<String, Schema> properties = schema.getProperties(); | ||
| for (Map.Entry<String, Schema> propertyEntry : properties.entrySet()) { | ||
| Schema propertySchema = propertyEntry.getValue(); | ||
| processNestedSchemas(propertySchema, visitedSchema); | ||
| } | ||
| } else if (ModelUtils.isAllOf(schema)) { | ||
| wrapComposedChildren(schema.getAllOf(), visitedSchema); | ||
| } else if (ModelUtils.isOneOf(schema)) { | ||
| wrapComposedChildren(schema.getOneOf(), visitedSchema); | ||
| } else if (ModelUtils.isAnyOf(schema)) { | ||
| wrapComposedChildren(schema.getAnyOf(), visitedSchema); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| private void wrapComposedChildren(List<Schema> children, Set<Schema> visitedSchema) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor suggestion: add a doc string explaining what this function does |
||
| if (children == null || children.isEmpty()) { | ||
| return; | ||
| } | ||
| for(Schema child: children) { | ||
| child = ModelUtils.getReferencedSchema(openAPI, child); | ||
| Map<String, Schema> properties = child.getProperties(); | ||
| if(properties == null || properties.isEmpty()) continue; | ||
| for(Map.Entry<String, Schema> propertyEntry : properties.entrySet()) { | ||
| Schema propertySchema = propertyEntry.getValue(); | ||
| processNestedSchemas(propertySchema, visitedSchema); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void preprocessOpenAPI(OpenAPI openAPI) { | ||
| super.preprocessOpenAPI(openAPI); | ||
| if (wrapComplexType) { | ||
| wrapModels(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Adds prefix to the enum allowable values | ||
| * NOTE: Enum values use C++ scoping rules, meaning that enum values are siblings of their type, not children of it. Therefore, enum value must be unique | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of updating this config file, what about creating a new one instead, e.g. protobuf-schema-config-wrapComplexType.yaml ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point. created new config file for config with wrapComplexType.