Repository navigation
[kotlin-spring] Escape generated values by source context #25766
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
base: master
Are you sure you want to change the base?
Changes from all commits
892cfd5
53fcf89
bbaf4d4
0d768fa
e557ee0
2ddfee0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
|
|
||
| package org.openapitools.codegen; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.github.benmanes.caffeine.cache.Cache; | ||
| import com.github.benmanes.caffeine.cache.Caffeine; | ||
| import com.github.benmanes.caffeine.cache.Ticker; | ||
|
|
@@ -4151,6 +4152,7 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo | |
|
|
||
| // unalias schema | ||
| p = unaliasSchema(p); | ||
| Object referencedDefault = p.getDefault(); | ||
|
|
||
| property.setSchemaIsFromAdditionalProperties(schemaIsFromAdditionalProperties); | ||
| property.required = required; | ||
|
|
@@ -4406,6 +4408,11 @@ public CodegenProperty fromProperty(String name, Schema p, boolean required, boo | |
| } | ||
| } | ||
|
|
||
| Object effectiveDefault = p.getDefault() != null ? p.getDefault() : referencedDefault; | ||
| property.hasDefaultValue = effectiveDefault != null; | ||
| property.rawDefaultValue = snapshotDefaultValue(effectiveDefault); | ||
| property.rawDefaultValueText = rawDefaultValueText(effectiveDefault); | ||
|
|
||
| // override defaultValue if it's not set and defaultToEmptyContainer is set | ||
| if (p.getDefault() == null && defaultToEmptyContainer) { | ||
| updateDefaultToEmptyContainer(property, p); | ||
|
|
@@ -4950,8 +4957,10 @@ public CodegenOperation fromOperation(String path, | |
| op.path = path; | ||
| } | ||
| // remove backslash from path, e.g. /api/v2/GetPetById\\(\\) => /api/v2/GetPetById() | ||
| op.unescapedPath = op.path; | ||
| op.path = op.path.replace("\\", ""); | ||
|
|
||
| op.unescapedSummary = operation.getSummary(); | ||
| op.summary = escapeText(operation.getSummary()); | ||
| op.unescapedNotes = operation.getDescription(); | ||
| op.notes = escapeText(operation.getDescription()); | ||
|
|
@@ -5128,6 +5137,7 @@ public CodegenOperation fromOperation(String path, | |
|
|
||
| if (bodyParam != null) { | ||
| bodyParam.description = escapeText(requestBody.getDescription()); | ||
| bodyParam.unescapedDescription = requestBody.getDescription(); | ||
| postProcessParameter(bodyParam); | ||
| bodyParams.add(bodyParam); | ||
| if (prependFormOrBodyParameters) { | ||
|
|
@@ -5329,6 +5339,7 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) { | |
| responseSchema = ModelUtils.getSchemaFromResponse(openAPI, response); | ||
| } | ||
| r.schema = responseSchema; | ||
| r.unescapedMessage = response.getDescription(); | ||
| r.message = escapeText(response.getDescription()); | ||
|
|
||
| // adding examples to API responses | ||
|
|
@@ -5892,11 +5903,38 @@ public CodegenParameter fromParameter(Parameter parameter, Set<String> imports) | |
|
|
||
| // set default value | ||
| codegenParameter.defaultValue = toDefaultParameterValue(codegenProperty, parameterSchema); | ||
|
|
||
| codegenParameter.hasDefaultValue = codegenProperty != null && codegenProperty.hasDefaultValue; | ||
| codegenParameter.rawDefaultValue = codegenProperty == null ? null : codegenProperty.rawDefaultValue; | ||
| codegenParameter.rawDefaultValueText = codegenProperty == null ? null : codegenProperty.rawDefaultValueText; | ||
|
Contributor
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. P2: Date form parameters can receive an invalid generated default in their Prompt for AI agents |
||
| // swagger-parser materializes date defaults as Date, losing the lexical | ||
| // OpenAPI value. Preserve the same date form emitted for parameter binding. | ||
| if (codegenParameter.isDate && codegenParameter.rawDefaultValue != null | ||
| && codegenParameter.defaultValue != null) { | ||
| codegenParameter.rawDefaultValueText = codegenParameter.defaultValue; | ||
| } | ||
| finishUpdatingParameter(codegenParameter, parameter); | ||
| return codegenParameter; | ||
| } | ||
|
|
||
| private JsonNode snapshotDefaultValue(Object value) { | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
| try { | ||
| return Json.mapper().valueToTree(value); | ||
| } catch (RuntimeException e) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private String rawDefaultValueText(Object value) { | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
| JsonNode node = snapshotDefaultValue(value); | ||
| return node != null && node.isTextual() ? node.textValue() : String.valueOf(value); | ||
| } | ||
|
|
||
| private Schema getReferencedSchemaWhenNotEnum(Schema parameterSchema) { | ||
| Schema referencedSchema = ModelUtils.getReferencedSchema(openAPI, parameterSchema); | ||
| if (isEnumSchema(referencedSchema)) { | ||
|
|
@@ -7256,7 +7294,9 @@ protected List<EnumVarMap> buildEnumVars(List<Object> values, String dataType, L | |
|
|
||
| final String finalEnumName = toEnumVarName(enumName, dataType); | ||
|
|
||
| enumVar.enumVar(finalEnumName, toEnumValue(String.valueOf(value), dataType), isDataTypeString(dataType)); | ||
| String rawEnumValue = String.valueOf(value); | ||
| enumVar.enumVar(finalEnumName, toEnumValue(rawEnumValue, dataType), isDataTypeString(dataType)); | ||
| enumVar.setEnumValueRaw(rawEnumValue); | ||
|
Contributor
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. P3: Prompt for AI agents |
||
| enumVar.setEnumRawValue(rawValues.get(i)); | ||
| // TODO: add isNumeric | ||
| enumVars.add(enumVar); | ||
|
|
@@ -7291,6 +7331,7 @@ private void injectEnumUnknownDefaultCase(List<EnumVarMap> enumVars, String data | |
| String.valueOf(11184809); | ||
|
|
||
| enumVar.enumVar(toEnumVarName(enumName, dataType), toEnumValue(enumValue, dataType), isDataTypeString(dataType)); | ||
| enumVar.setEnumValueRaw(enumValue); | ||
| enumVar.setEnumRawValue(enumValue); | ||
| // TODO: add isNumeric | ||
| enumVars.add(enumVar); | ||
|
|
@@ -7645,6 +7686,7 @@ private void setOauth2Info(CodegenSecurity codegenSecurity, OAuthFlow flow) { | |
| Map<String, Object> scope = new HashMap<>(); | ||
| scope.put("scope", scopeEntry.getKey()); | ||
| scope.put("description", escapeText(scopeEntry.getValue())); | ||
| scope.put("descriptionRaw", scopeEntry.getValue()); | ||
| scopes.add(scope); | ||
| } | ||
| codegenSecurity.scopes = scopes; | ||
|
|
@@ -7933,6 +7975,9 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set | |
|
|
||
| // set default value | ||
| codegenParameter.defaultValue = toDefaultParameterValue(codegenProperty, propertySchema); | ||
| codegenParameter.hasDefaultValue = codegenProperty != null && codegenProperty.hasDefaultValue; | ||
| codegenParameter.rawDefaultValue = codegenProperty == null ? null : codegenProperty.rawDefaultValue; | ||
| codegenParameter.rawDefaultValueText = codegenProperty == null ? null : codegenProperty.rawDefaultValueText; | ||
|
|
||
| if (ModelUtils.isFileSchema(ps) && !ModelUtils.isStringSchema(ps)) { | ||
| // swagger v2 only, type file | ||
|
|
@@ -8053,7 +8098,7 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set | |
|
|
||
| codegenParameter.isFormParam = Boolean.TRUE; | ||
| codegenParameter.description = escapeText(codegenProperty.description); | ||
| codegenParameter.unescapedDescription = codegenProperty.getDescription(); | ||
| codegenParameter.unescapedDescription = codegenProperty.unescapedDescription; | ||
| codegenParameter.jsonSchema = Json.pretty(propertySchema); | ||
| codegenParameter.containerType = codegenProperty.containerType; | ||
| codegenParameter.containerTypeMapped = codegenProperty.containerTypeMapped; | ||
|
|
@@ -8083,7 +8128,6 @@ public CodegenParameter fromFormProperty(String name, Schema propertySchema, Set | |
|
|
||
| // set nullable | ||
| setParameterNullable(codegenParameter, codegenProperty); | ||
|
|
||
| return codegenParameter; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.